版本更新
最近 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
,当初大家想在本地体验 GScript
的 REPL
时也只须要运行Docker
就能应用。
docker pull crossoverjie/gscriptdocker 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);
其中的实现源码基本上是借鉴了 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 源码:https://github.com/crossoverJie/gscript
- Playground 源码:https://github.com/crossoverJie/gscript-homepage
- GScript Docker地址:https://hub.docker.com/r/crossoverjie/gscript