sptc 0.0.24 → 0.0.26

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/engine/index.js CHANGED
@@ -24,6 +24,7 @@ function executeSptcFile(filename, payload, option={}) {
24
24
  const [ctx0, vm]=Compiler.compileSptcFile(filename, _option)
25
25
  const [ctx, priv]=Interpreter.buildContext({...payload, ...ctx0}, option)
26
26
  Interpreter.executeVm(vm, ctx, priv)
27
+ return ctx
27
28
  }
28
29
 
29
30
  function executeSptcFileEx(...x) {
@@ -53,6 +54,14 @@ function executeSptcMacroFile(filename, config, mockContent) {
53
54
  return InterpreterMacro.executeMacroContext(ctx, ast)
54
55
  }
55
56
 
57
+ function includeJsFile(filename, payload) {
58
+ return Interpreter.buildContext({}, {})[0].include_js(filename, payload)
59
+ }
60
+
61
+ function includeSptcFile(filename, payload) {
62
+ return executeSptcFile(filename, payload)
63
+ }
64
+
56
65
  module.exports={
57
66
  ...Compiler,
58
67
  ...Interpreter,
@@ -61,5 +70,7 @@ module.exports={
61
70
  executeSptcFile,
62
71
  executeSptcFileEx,
63
72
  executeSptcMacroFile,
73
+ includeSptcFile,
74
+ includeJsFile,
64
75
  version: require('../package.json').version
65
76
  }
@@ -1,4 +1,5 @@
1
1
  const {compileSptcMacroFile, MACRO_TYPES}=require('./compiler-macro')
2
+ const path=require('path')
2
3
 
3
4
  const {
4
5
  O_IFDEF,
@@ -58,7 +59,7 @@ function execute(ctx, ast) {
58
59
  }else if(p.type===O_INCLUDE) {
59
60
  const filename=path.resolve(ctx.filename+'/..', p.include)
60
61
  const nctx=Object.assign({}, ctx, {filename})
61
- ret+=executeContext(nctx)
62
+ ret+=executeMacroContext(nctx)
62
63
  }else{
63
64
  throw new Error('unsupported node: '+JSON.stringify(p))
64
65
  }
@@ -277,6 +277,18 @@ function buildContext(ctx0, option) {
277
277
  })
278
278
  }
279
279
 
280
+ ctx.include_js=(inc_filename, inc_payload={})=>{
281
+ const inc=path.resolve(ctx0.__dirname, inc_filename)
282
+ const jsvm=Utils.compileFile(inc, {
283
+ compileFunc: code=>new vm.Script(code+'; Symbol()[0]=_=>0', inc)
284
+ })
285
+ const jsctx={
286
+ ...inc_payload,
287
+ }
288
+ jsvm.runInNewContext(jsctx)
289
+ return jsctx
290
+ }
291
+
280
292
  ctx.__autoload=fn=>{
281
293
  priv.__autoload_func=fn
282
294
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "sptc",
3
- "version": "0.0.24",
3
+ "version": "0.0.26",
4
4
  "description": "Simple Pretreat Toolkit CLI",
5
- "main": "",
5
+ "main": "engine/index.js",
6
6
  "engines": {
7
7
  "node": ">=12"
8
8
  },
package/tests/ctx/1.js ADDED
@@ -0,0 +1,6 @@
1
+ const sptc=require('../..')
2
+ const {sub}=sptc.includeSptcFile(__dirname+'/a.s', {w: 100})
3
+ const {add}=sptc.includeJsFile(__dirname+'/a.js', {w: 500})
4
+
5
+ console.log('add', add(3, 4))
6
+ console.log('sub', sub(10, 7))
package/tests/ctx/a.js ADDED
@@ -0,0 +1,3 @@
1
+ function add(x, y) {
2
+ return w+x+y
3
+ }
package/tests/ctx/a.s ADDED
@@ -0,0 +1,4 @@
1
+ <?js
2
+ function sub(x, y) {
3
+ return w+x-y
4
+ }
@@ -0,0 +1,3 @@
1
+ <?js
2
+ const mod=include_js(__dirname+'/a.js', {c: 100})
3
+ console.log(mod.add(3,4))
@@ -0,0 +1,3 @@
1
+ function add(x, y) {
2
+ return c+x+y
3
+ }
@@ -1,7 +1,7 @@
1
1
  const {mtime, readTextFile}=require('./base')
2
2
 
3
3
  const _caches={}
4
- function compileFile(filename, option) {
4
+ function compileFile(filename, option={}) {
5
5
  const {
6
6
  contentWrapper=x=>x,
7
7
  mockFileContent=null,