GScript 编写标准库示例详解

2022-10-17 18:19:56
目录
版本更新引言使用 Docker编写 GScript 标准库

版本更新

最近>GScript 更新了 v0.0.11 版本,重点更新了:

    Docker 运行环境新增了 byte 原始类型新增了一些字符串标准库 Strings/StringBuilder数组切片语法:int[] b = a[1: len(a)];

    引言

    前段时间发布了>GScript 的在线 playground

    这是一个可以在线运行 GScript 脚本的网站,其本质原理是接收用户的输入源码从而在服务器上运行的服务;这简直就是后门大开的 XSS 攻击,为保住服务器我设置了运行 API 的后端服务的用户权限,这样可以避免执行一些恶意的请求。

    但也避免不了一些用户执行了一些耗时操作,比如一个死循环、或者是我提供 demo 里的打印杨辉三角。

    这本质上是一个递归函数,当打印的三角层数过高时便会非常耗时,同时也非常消耗 CPU。

    有几次我去检查服务器时发现了几个 CPU 过高的进程,基本上都是这样的耗时操作,不可避免的会影响到服务器的性能。

    使用>

    为了解决这类问题,很自然的就能想到可以使用 Docker,所有的资源都和宿主机是隔离开的,无论怎么瞎折腾也不会影响到宿主机。

    说干就干,最后修改了 API 执行脚本的地方:

        string fileName = d.unix("Asia/Shanghai") + "temp.gs" ;
        s.writeFile(fileName, body, 438);
        string pwd = s.getwd();
        // string res = s.command("gscript", fileName);
        string res = s.command("docker","run","--rm","-v", pwd+":/usr/src/gscript","-w","/usr/src/gscript", "crossoverjie/gscript","gscript", fileName);
        s.remove(fileName);
        r.body = res;
        r.ast = dumpAST(body);
        r.symbol=dumpSymbol(body);
        ctx.JSON(200, r);
    

    主要修改的就是将直接执行的 GScript 命令修改为了调用 docker 执行。

    但其实也还有改进空间,后续新增协程之后可以便可监控运行时间,超时后便会自动 kill 进程。

    我也将该 Docker 上传到了 DockerHub,现在大家想在本地体验 GScriptREPL 时也只需要运行Docker 就能使用。

    docker pull crossoverjie/gscript
    docker run --rm -it  crossoverjie/gscript:latest gscript
    

    当然也可以执行用 Docker 执行 GScript 脚本:

    docker run --rm -v $PWD:/usr/src/gscript -w /usr/src/gscript crossoverjie/gscript gscript {yourpath}/temp.gs
    

    编写>

    接下来重点聊聊 GScript 标准库的事情,其实编写标准库是一个费时费力的事情。

    现在编译器已经提供了一些可用的内置函数,借由这些内置函数写一些常见的工具类是完全没有问题的。

    对写 GScript 标准库感谢的朋友可以当做一个参考,这里我打了一个样,先看下运行效果:

    // 字符串工具类
    StringBuilder b = StringBuilder();
    b.writeString("10");
    b.writeString("20");
    int l = b.writeString("30");
    string s = b.String();
    printf("s:%s, len=%d ",s,l);
    assertEqual(s,"102030");
    byte[] b2 = toByteArray("40");
    b.WriteBytes(b2);
    s = b.String();
    assertEqual(s,"10203040");
    println(s);
    // Strings 工具类
    Strings s = Strings();
    string[] elems = {"name=xxx","age=xx"};
    string ret = s.join(elems, "&");
    println(ret);
    assertEqual(ret, "name=xxx&age=xx");
    bool b = s.hasPrefix("http://www.xx.com", "http");
    println(b);
    assertEqual(b,true);
    b = s.hasPrefix("http://www.xx.com", "https");
    println(b);
    assertEqual(b,false);
    

    其中的实现源码基本上是借鉴了 Go 的标准库,先来看看 StringBuilder 的源码:

    class StringBuilder{
        byte[] buf = [0]{};
        // append contents to buf, it returns the length of s
        int writeString(string s){
            byte[] temp = toByteArray(s);
            append(buf, temp);
            return len(temp);
        }
        // append b to buf, it returns the length of b.
        int WriteBytes(byte[] b){
            append(buf, b);
            return len(b);
        }
        // copies the buffer to a new.
        grow(int n){
            if (n > 0) {
                // when there is not enough space left.
                if (cap(buf) - len(buf) < n) {
                    byte[] newBuf = [len(buf), 2*cap(buf)+n]{};
                    copy(newBuf, buf);
                    buf = newBuf;
                }
            }   
        }
        string String(){
            return toString(buf);
        }
    }
    

    主要就是借助了原始的数组类型以及 toByteArray/toString 字节数组和字符串的转换函数实现的。

    class Strings{
        // concatenates the elements of its first argument to create a single string. The separator
        // string sep is placed between elements in the resulting string.
        string join(string[] elems, string sep){
            if (len(elems) == 0) {
                return "";
            }
            if (len(elems) == 1) {
                return elems[0];
            }
            byte[] bs = toByteArray(sep);
            int n = len(bs) * (len(elems) -1);
            for (int i=0; i < len(elems); i++) {
                string s = elems[i];
                byte[] bs = toByteArray(s);
                n = n + len(bs);
            }
            StringBuilder sb = StringBuilder();
            sb.grow(n);
            string first = elems[0];
            sb.writeString(first);
            string[] remain = elems[1:len(elems)];
            for(int i=0; i < len(remain); i++){
                sb.writeString(sep);
                string r = remain[i];
                sb.writeString(r);
            }
            return sb.String();
        }
        // tests whether the string s begins with prefix.
        bool hasPrefix(string s, string prefix){
            byte[] bs = toByteArray(s);
            byte[] bp = toByteArray(prefix);    
            return len(bs) >= len(bp) && toString(bs[0:len(bp)]) == prefix;
        }
    }
    

    Strings 工具类也是类似的,都是一些内置函数的组合运用;

    在写标准库的过程中还会有额外收获,可以再次阅读一遍 Go 标准库的实现流程,换了一种语法实现出来,会加深对 Go 标准库的理解。

    所以欢迎感兴趣的朋友向 GScript 贡献标准库,由于我个人精力有限,实现过程中可能会发现缺少某些内置函数或数据结构,这也没关系,反馈 issue 后我会尽快处理。

    由于目前 GScript 还不支持包管理,所以新增的函数可以创建 Class 来实现,后续支持包或者是 namespace 之后直接将该 Class 迁移过去即可。

    本文相关资源链接

      GScript 源码:github.com/crossoverJi…Playground 源码:github.com/crossoverJi…GScript Docker地址:hub.docker.com/r/crossover…

      以上就是GScript 编写标准库示例详解的详细内容,更多关于GScript 编写标准库的资料请关注易采站长站其它相关文章!