Skip to content

Commit 71d7d04

Browse files
authored
Merge pull request #3 from a876691666/main
Refactoring eval to Function
2 parents fc47bf0 + 05a1d46 commit 71d7d04

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

.prettierrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"tabWidth": 4,
33
"useTabs": false,
44
"semi": true,
5-
"printWidth": 100
5+
"printWidth": 100,
6+
"singleQuote": true,
7+
"bracketSpacing": true,
8+
"arrowParens": "always",
9+
"trailingComma": "all",
10+
"bracketSameLine": false
611
}

src/export/Script.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Node } from '../nodes/Node';
55

66
/**
77
* When writing to the script, this object represents the "scope" in which the script is in... for example, defining a variable at the root
8-
* will declare the variable at a diferent place than if you are inside of an If statement.
8+
* will declare the variable at a diferent place than if you are inside of an If statement.
99
*/
1010
type Scope = {
1111
parent?: Scope;
@@ -222,7 +222,7 @@ export class Script {
222222
) {
223223
functionName = 'fn_' + functionName;
224224

225-
this.importModule("Fn");
225+
this.importModule('Fn');
226226

227227
if (!this.isDefined(functionName, this.rootScope)) {
228228
this.rootScope.definitions.push([
@@ -346,29 +346,10 @@ export class Script {
346346
return `texture${index}`;
347347
}
348348

349-
toString(lastExpression: string = '', forExport = false) {
350-
let output = `\n`;
349+
// 主过程转换为字符串
350+
toProcessString(forExport = false) {
351351
const exportKeyword = forExport ? 'export' : '';
352-
353-
if (forExport) {
354-
output += "import * as THREE from 'three/webgpu';\n";
355-
356-
//
357-
// Imports...
358-
//
359-
for (const module in this.imports)
360-
output += `\nimport {${[...this.imports[module]].join(',')}} from '${module}';
361-
`;
362-
} else {
363-
output += ` const THREE = fromModule('THREE');
364-
`;
365-
//
366-
// since the script will be evaluated we will need to be injected by the evaluator...
367-
//
368-
for (const module in this.imports)
369-
output += `\n const {${[...this.imports[module]].join(',')}} = fromModule('${module}');
370-
`;
371-
}
352+
let output = '';
372353

373354
//
374355
// uniforms
@@ -453,16 +434,60 @@ const texture${index} = loadTexture('${forExport ? path : previewUrl}', '${mime}
453434

454435
output += this.writeScopeDefinitions(this.rootScope);
455436

437+
return output;
438+
}
439+
440+
toString(lastExpression: string = '', forExport = false) {
441+
let output = `\n`;
442+
443+
if (forExport) {
444+
output += "import * as THREE from 'three/webgpu';\n";
445+
446+
//
447+
// Imports...
448+
//
449+
for (const module in this.imports)
450+
output += `\nimport {${[...this.imports[module]].join(',')}} from '${module}';
451+
`;
452+
} else {
453+
output += ` const THREE = fromModule('THREE');
454+
`;
455+
//
456+
// since the script will be evaluated we will need to be injected by the evaluator...
457+
//
458+
for (const module in this.imports)
459+
output += `\n const {${[...this.imports[module]].join(',')}} = fromModule('${module}');
460+
`;
461+
}
462+
463+
output += this.toProcessString(forExport);
464+
456465
return output + `\n${lastExpression};`;
457466
}
458467

459468
eval(returnThisRef: string) {
460-
const fromModule = (modulePath: string) =>
461-
this.moduleName2Ref[modulePath];
469+
const stringFunc = this.toProcessString(false);
470+
471+
const importKeys = Object.values(this.imports)
472+
.map((item) => Array.from(item.values()))
473+
.flat();
474+
475+
const importValues = Object.entries(this.imports)
476+
.map((item) => {
477+
const module: any = this.moduleName2Ref[item[0]];
478+
return Array.from(item[1].values()).map((name) => module[name]);
479+
})
480+
.flat();
481+
482+
const _function = new Function(
483+
'THREE',
484+
...importKeys,
485+
stringFunc + `\n;return ${returnThisRef};`,
486+
);
462487

463-
fromModule(''); //Uff... i'll see how to improve this later.
488+
const material = _function(THREE, ...importValues);
464489

465-
return eval(this.toString(returnThisRef, false));
490+
return material;
466491
}
467492

468493
public static makeValidVariableName(input: string) {

0 commit comments

Comments
 (0)