sptc 0.0.11 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/sptcd.js +1 -1
- package/engine/index.js +9 -2
- package/engine/interpreter.js +18 -17
- package/package.json +1 -1
- package/tests/www/stream.s +4 -0
- package/utils/base.js +10 -0
package/bin/sptcd.js
CHANGED
package/engine/index.js
CHANGED
|
@@ -28,14 +28,21 @@ function executeSptcFile(filename, payload, option={}) {
|
|
|
28
28
|
|
|
29
29
|
function executeSptcFileEx(...x) {
|
|
30
30
|
const e=new Promise((resolve, reject)=>{
|
|
31
|
-
const {end, onError
|
|
31
|
+
const {end, onError}=(x[2]=x[2] || {})
|
|
32
32
|
const buf=[]
|
|
33
33
|
x[2].write=(...x)=>{
|
|
34
34
|
buf.push(...x)
|
|
35
35
|
}
|
|
36
36
|
x[2].end=_=>{
|
|
37
37
|
end && end()
|
|
38
|
-
|
|
38
|
+
const xbuf=[]
|
|
39
|
+
const is_str=typeof buf[0]==='string'
|
|
40
|
+
for(let x of buf) {
|
|
41
|
+
if(is_str && typeof x!=='string') x=x.toString('utf8')
|
|
42
|
+
if(!is_str && typeof x==='string') x=Buffer.concat(x)
|
|
43
|
+
xbuf.push(x)
|
|
44
|
+
}
|
|
45
|
+
resolve(xbuf)
|
|
39
46
|
}
|
|
40
47
|
x[2].onError=e=>{
|
|
41
48
|
onError && onError(e)
|
package/engine/interpreter.js
CHANGED
|
@@ -2,7 +2,7 @@ const _module=require('module')
|
|
|
2
2
|
const vm=require('vm')
|
|
3
3
|
const path=require('path')
|
|
4
4
|
const Compiler=require('./compiler')
|
|
5
|
-
const {generate_uuid}=require('../utils')
|
|
5
|
+
const {generate_uuid, PromiseWithResolvers}=require('../utils')
|
|
6
6
|
|
|
7
7
|
/*
|
|
8
8
|
const tt={x: 2, b: [2, 5], c: _=>2, d: /xx/ig}
|
|
@@ -132,7 +132,6 @@ function buildContext(ctx0, option) {
|
|
|
132
132
|
end=NOOP,
|
|
133
133
|
onError=NOOP,
|
|
134
134
|
masterPriv=null,
|
|
135
|
-
readAsBinary=false,
|
|
136
135
|
isEntry=false,
|
|
137
136
|
}=option
|
|
138
137
|
|
|
@@ -174,7 +173,7 @@ function buildContext(ctx0, option) {
|
|
|
174
173
|
ctx.flush=_=>{
|
|
175
174
|
if(priv.isEnd) return;
|
|
176
175
|
const {echos}=priv
|
|
177
|
-
echos.splice(0).map(x=>write(toWritable(x
|
|
176
|
+
echos.splice(0).map(x=>write(toWritable(x)))
|
|
178
177
|
}
|
|
179
178
|
|
|
180
179
|
ctx.var_dump=(...x)=>{
|
|
@@ -191,6 +190,11 @@ function buildContext(ctx0, option) {
|
|
|
191
190
|
}
|
|
192
191
|
priv.syncs.push(...x)
|
|
193
192
|
},
|
|
193
|
+
Lock: _=>{
|
|
194
|
+
const lock=PromiseWithResolvers()
|
|
195
|
+
priv.syncs.push(lock.promise)
|
|
196
|
+
return _=>lock.resolve()
|
|
197
|
+
},
|
|
194
198
|
}
|
|
195
199
|
|
|
196
200
|
ctx.defer=fn=>{
|
|
@@ -235,7 +239,6 @@ function buildContext(ctx0, option) {
|
|
|
235
239
|
write: option.write,
|
|
236
240
|
onError: option.onError,
|
|
237
241
|
masterPriv: priv,
|
|
238
|
-
readAsBinary,
|
|
239
242
|
isEntry: false,
|
|
240
243
|
})
|
|
241
244
|
const ret=executeVm(_vm, _ctx, priv)
|
|
@@ -281,21 +284,18 @@ function buildContext(ctx0, option) {
|
|
|
281
284
|
return [pctx, priv]
|
|
282
285
|
}
|
|
283
286
|
|
|
284
|
-
function toWritable(x
|
|
287
|
+
function toWritable(x) {
|
|
285
288
|
if(x && [Uint8Array, Buffer].includes(x.constructor)) {
|
|
286
|
-
return
|
|
289
|
+
return x
|
|
287
290
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
if(
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return JSON.stringify(x)+''
|
|
297
|
-
})()
|
|
298
|
-
return asBinary? Buffer.from(str): str
|
|
291
|
+
if(typeof x==='string') return x
|
|
292
|
+
if(typeof x==='undefined') return 'undefined'
|
|
293
|
+
if(typeof x==='number') {
|
|
294
|
+
if(isNaN(x))return 'NaN'
|
|
295
|
+
if(x===Infinity) return 'Infinity'
|
|
296
|
+
if(x===-Infinity) return '-Infinity'
|
|
297
|
+
}
|
|
298
|
+
return JSON.stringify(x)+''
|
|
299
299
|
}
|
|
300
300
|
|
|
301
301
|
function executeVm(vm, ctx, priv) {
|
|
@@ -318,6 +318,7 @@ function executeVm(vm, ctx, priv) {
|
|
|
318
318
|
onError(e)
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
+
if(!isMaster) return;
|
|
321
322
|
for(let fn of priv._defers.splice(0)) {
|
|
322
323
|
try{
|
|
323
324
|
fn()
|
package/package.json
CHANGED
package/utils/base.js
CHANGED
|
@@ -53,6 +53,15 @@ function getLocalIpv4Addresses() {
|
|
|
53
53
|
return Object.keys(rr)
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
function PromiseWithResolvers() {
|
|
57
|
+
let _resolve, _reject
|
|
58
|
+
const promise=new Promise((resolve, reject)=>{
|
|
59
|
+
_resolve=resolve
|
|
60
|
+
_reject=reject
|
|
61
|
+
})
|
|
62
|
+
return {promise, resolve: _resolve, reject: _reject}
|
|
63
|
+
}
|
|
64
|
+
|
|
56
65
|
module.exports={
|
|
57
66
|
readTextFile,
|
|
58
67
|
mtime,
|
|
@@ -62,4 +71,5 @@ module.exports={
|
|
|
62
71
|
generate_uuid,
|
|
63
72
|
getExtension,
|
|
64
73
|
getLocalIpv4Addresses,
|
|
74
|
+
PromiseWithResolvers,
|
|
65
75
|
}
|