版别更新

最近 GScript 更新了 v0.0.11 版别,要点更新了:

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

详细更新内容请看下文。

前言

前段时刻发布了 GScript 的在线 playground

这是一个能够在线运转 GScript 脚本的网站,其本质原理是接纳用户的输入源码从而在服务器上运转的服务;这简直便是后门大开的 XSS 进犯,为保住服务器我设置了运转 API 的后端服务的用户权限,这样能够防止履行一些歹意的请求。

但也防止不了一些用户履行了一些耗时操作,比方一个死循环、或者是我供给 demo 里的打印杨辉三角。

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

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

运用 Docker

为了处理这类问题,很自然的就能想到能够运用 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 规范库的事情,其实编写规范库是一个费时费力的事情。

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

对写 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);

bool b = s.hasPrefix("www.xx.com", "http"); println(b); assertEqual(b,true); b = s.hasPrefix("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 编写规范库