Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc59afe9

Browse files
PavloPavlo
Pavlo
authored and
Pavlo
committed
fixed module loader conflict with package Loader
1 parentf7c906d commitc59afe9

File tree

6 files changed

+75
-15
lines changed

6 files changed

+75
-15
lines changed

‎index.html

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h4>JSPython development console</h4>
5353
resultEditor.session.setMode("ace/mode/json");
5454

5555
constinterpreter=jspython.jsPython();
56-
console.log({interpreter});
56+
console.log({interpreter});
5757

5858
functiontokenize(){
5959
tokenizer=(s)=>console.log(`tokens =>${s}`,interpreter().tokenize(s))
@@ -95,14 +95,14 @@ <h4>JSPython development console</h4>
9595

9696
interpreter.registerModuleLoader((path=>{
9797
returnPromise.resolve(`
98+
from 'service' import add
99+
98100
def multiply(x, y):
99101
x * y
100102
101103
def func1(x, y):
102-
if y == null:
103-
y = 77
104-
105-
multiply(x, y) + someNumber
104+
print('++>>>', getExecutionContext())
105+
multiply(x, y) + add(x, y)
106106
107107
name = 'test'
108108
someNumber = 55
@@ -111,6 +111,16 @@ <h4>JSPython development console</h4>
111111
`);
112112
}));
113113

114+
interpreter.registerPackagesLoader(path=>
115+
(
116+
path==='service' ?{
117+
add:(x,y)=>x+y,
118+
remove:(x,y)=>x-y,
119+
times:(x,y)=>x*y,
120+
}
121+
:null
122+
)
123+
);
114124
constscope={
115125
Math,
116126
errorFunc:p=>{

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"jspython-interpreter",
3-
"version":"2.0.18",
3+
"version":"2.1.1",
44
"description":"JSPython is a javascript implementation of Python language that runs within web browser or NodeJS environment",
55
"keywords": [
66
"python",

‎src/evaluator/evaluatorAsync.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import { BlockContext, cloneContext, Scope } from './scope';
1818
exportclassEvaluatorAsync{
1919

2020
privatemoduleParser:(modulePath:string)=>Promise<AstBlock>=()=>Promise.reject('Module parser is not registered!');
21-
privateblockContextFactory?:(modulePath:string)=>BlockContext;
21+
privateblockContextFactory?:(modulePath:string,ast:AstBlock)=>BlockContext;
2222

2323
registerModuleParser(moduleParser:(modulePath:string)=>Promise<AstBlock>):EvaluatorAsync{
2424
this.moduleParser=moduleParser;
2525
returnthis;
2626
}
2727

28-
registerBlockContextFactory(blockContextFactory:(modulePath:string)=>BlockContext):EvaluatorAsync{
28+
registerBlockContextFactory(blockContextFactory:(modulePath:string,ast:AstBlock)=>BlockContext):EvaluatorAsync{
2929
this.blockContextFactory=blockContextFactory;
3030
returnthis;
3131
}
@@ -51,12 +51,17 @@ export class EvaluatorAsync {
5151
if(node.type==='import'){
5252
constimportNode=nodeasImportNode;
5353

54+
if(!importNode.module.name.startsWith('/')/* || !importNode.module.name.endsWith('.jspy')*/){
55+
// it is not JSPY imort. It is JS and should be handled externally
56+
continue;
57+
}
58+
5459
if(typeofthis.blockContextFactory!=='function'){
5560
thrownewError('blockContextFactory is not initialized');
5661
}
5762

5863
constmoduleAst=awaitthis.moduleParser(importNode.module.name)
59-
constmoduleBlockContext=this.blockContextFactory(importNode.module.name);
64+
constmoduleBlockContext=this.blockContextFactory(importNode.module.name,moduleAst);
6065
awaitthis.evalBlockAsync(moduleAst,moduleBlockContext)
6166

6267
blockContext.blockScope.set(importNode.module.alias||this.defaultModuleName(importNode.module.name),moduleBlockContext.blockScope.getScope())

‎src/initialScope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parseDatetimeOrNull } from "./common/utils";
22

33
exportconstINITIAL_SCOPE={
44
jsPython():string{
5-
return[`JSPython v2.0.17`,"(c) 2021 FalconSoft Ltd. All rights reserved."].join('\n')
5+
return[`JSPython v2.1.1`,"(c) 2021 FalconSoft Ltd. All rights reserved."].join('\n')
66
},
77
dateTime:(str:number|string|any=null)=>parseDatetimeOrNull(str)||newDate(),
88
range:range,

‎src/interpreter.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,5 +676,42 @@ describe('Interpreter', () => {
676676
expect(res).toBe(316);
677677
});
678678

679+
it('Import with package loader',async()=>{
680+
constinterpreter=Interpreter.create();
681+
682+
interpreter.registerPackagesLoader(path=>
683+
(
684+
path==='service' ?{
685+
add:(x,y)=>x+y,
686+
remove:(x,y)=>x-y,
687+
times:(x,y)=>x*y,
688+
}
689+
:null
690+
)
691+
);
692+
693+
interpreter.registerModuleLoader((path=>{
694+
returnPromise.resolve(`
695+
from 'service' import add
696+
697+
def multiply(x, y):
698+
x * y
699+
700+
def func1(x, y):
701+
add(x, y) + someNumber
702+
703+
someNumber = 55
704+
`);
705+
}));
706+
707+
constres=awaitinterpreter.evaluate(`
708+
import '/service.jspy' as obj
709+
710+
return obj.func1(2, 3)
711+
`);
712+
713+
expect(res).toBe(60);
714+
});
715+
679716

680717
});

‎src/interpreter.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,16 @@ export class Interpreter {
9393
this._lastExecutionContext=blockContext.blockScope.getScope();
9494

9595
constresult=awaitevaluator
96-
.registerModuleParser(async(modulePath)=>awaitthis.moduleParser(modulePath))
97-
.registerBlockContextFactory((moduleName)=>({ moduleName,blockScope:newScope(scope)}))
96+
.registerModuleParser(async(modulePath)=>awaitthis.moduleParser(modulePath))
97+
.registerBlockContextFactory((moduleName,ast:AstBlock)=>{
98+
// this line will not be required when we have move package loaders to the evaluator
99+
constnewContext=this.assignLegacyImportContext(ast,scope);
100+
101+
constmoduleContext={ moduleName,blockScope:newScope(newContext)}
102+
moduleContext.blockScope.set('printExecutionContext',()=>console.log(moduleContext.blockScope.getScope()));
103+
moduleContext.blockScope.set('getExecutionContext',()=>moduleContext.blockScope.getScope());
104+
returnmoduleContext;
105+
})
98106
.evalBlockAsync(ast,blockContext);
99107

100108
if(!entryFunctionName||!entryFunctionName.length){
@@ -117,7 +125,7 @@ export class Interpreter {
117125
constast=this.parse(script,moduleName);
118126

119127
context=(context&&typeofcontext==='object') ?context :{};
120-
context=awaitthis.assignLegacyImportContext(ast,context);
128+
context=this.assignLegacyImportContext(ast,context);
121129

122130
constglobalScope={
123131
...this.initialScope,
@@ -128,7 +136,7 @@ export class Interpreter {
128136
}
129137

130138

131-
privateasyncassignLegacyImportContext(ast:AstBlock,context:object):Promise<object>{
139+
privateassignLegacyImportContext(ast:AstBlock,context:object):Record<string,unknown>{
132140
constimportNodes=ast.body.filter(n=>n.type==='import')asImportNode[];
133141

134142
constjsImport=importNodes
@@ -140,7 +148,7 @@ export class Interpreter {
140148
context={ ...context, ...libraries};
141149
}
142150

143-
returncontext;
151+
returncontextasRecord<string,unknown>;
144152
}
145153

146154
registerPackagesLoader(loader:PackageLoader){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp