1- import { SandboxContext , ScriptContext } from " @App/apps/grant/frontend" ;
2- import { ScriptCache , Script } from " @App/model/do/script" ;
1+ import { FrontenApiValue , SandboxContext , ScriptContext } from ' @App/apps/grant/frontend' ;
2+ import { ScriptCache , Script } from ' @App/model/do/script' ;
33
4+ // 编译脚本代码字符串
45export function compileScriptCode ( script : ScriptCache ) : string {
56 let code = script . code ;
67 let require = '' ;
78 script . metadata [ 'require' ] && script . metadata [ 'require' ] . forEach ( ( val ) => {
8- let res = script . resource [ val ] ;
9+ const res = script . resource [ val ] ;
910 if ( res ) {
10- require = require + "\n" + res . content ;
11+ require = require + '\n' + res . content ;
1112 }
1213 } ) ;
1314 code = require + code ;
@@ -16,49 +17,52 @@ export function compileScriptCode(script: ScriptCache): string {
1617 '\n})(context)'
1718}
1819
19- export function compileScript ( script : ScriptCache ) : Function {
20+ // 编译成脚本方法
21+ export function compileScript ( script : ScriptCache ) : any {
2022 return new Function ( 'context' , script . code ) ;
2123}
2224
23- function setDepend ( context : ScriptContext , apiVal : { [ key : string ] : any } ) {
25+ // 设置api依赖
26+ function setDepend ( context : ScriptContext , apiVal : FrontenApiValue ) {
2427 if ( apiVal . param . depend ) {
2528 for ( let i = 0 ; i < apiVal . param . depend . length ; i ++ ) {
26- let value = apiVal . param . depend [ i ] ;
27- let dependApi = context . getApi ( value ) ;
29+ const value = apiVal . param . depend [ i ] ;
30+ const dependApi = context . getApi ( value ) ;
2831 if ( ! dependApi ) {
2932 return ;
3033 }
31- if ( value . startsWith ( " GM." ) ) {
32- let [ _ , t ] = value . split ( "." ) ;
33- context [ "GM" ] [ t ] = dependApi . api ;
34+ if ( value . startsWith ( ' GM.' ) ) {
35+ const [ _ , t ] = value . split ( '.' ) ;
36+ ( < { [ key : string ] : any } > context [ 'GM' ] ) [ t ] = dependApi . api . bind ( context ) ;
3437 } else {
35- context [ value ] = dependApi . api ;
38+ context [ value ] = dependApi . api . bind ( context ) ;
3639 }
3740 setDepend ( context , dependApi ) ;
3841 }
3942 }
4043}
4144
45+ // 创建沙盒
4246export function createSandboxContext ( script : ScriptCache ) : SandboxContext {
43- let context : SandboxContext = new SandboxContext ( script ) ;
47+ const context : SandboxContext = new SandboxContext ( script ) ;
4448 return < SandboxContext > createContext ( context , script ) ;
4549}
4650
4751export function createContext ( context : ScriptContext , script : Script ) : ScriptContext {
4852 context [ 'postRequest' ] = context . postRequest ;
4953 context [ 'script' ] = context . script ;
50- if ( script . metadata [ " grant" ] ) {
51- context [ "GM" ] = context ;
52- script . metadata [ " grant" ] . forEach ( ( value : any ) => {
53- let apiVal = context . getApi ( value ) ;
54+ if ( script . metadata [ ' grant' ] ) {
55+ context [ 'GM' ] = context ;
56+ script . metadata [ ' grant' ] . forEach ( ( value : string ) => {
57+ const apiVal = context . getApi ( value ) ;
5458 if ( ! apiVal ) {
5559 return ;
5660 }
57- if ( value . startsWith ( " GM." ) ) {
58- let [ _ , t ] = value . split ( "." ) ;
59- context [ "GM" ] [ t ] = apiVal . api ;
61+ if ( value . startsWith ( ' GM.' ) ) {
62+ const [ _ , t ] = value . split ( '.' ) ;
63+ ( < { [ key : string ] : any } > context [ 'GM' ] ) [ t ] = apiVal . api . bind ( context ) ;
6064 } else {
61- context [ value ] = apiVal . api ;
65+ context [ value ] = apiVal . api . bind ( context ) ;
6266 }
6367 setDepend ( context , apiVal ) ;
6468 } ) ;
0 commit comments