fix(execute_script): route to own scene script eval instead of nonexistent 'console'
This commit is contained in:
@@ -2,6 +2,30 @@ import { join } from 'path';
|
||||
module.paths.push(join(Editor.App.path, 'node_modules'));
|
||||
|
||||
export const methods: { [key: string]: (...any: any) => any } = {
|
||||
/**
|
||||
* Execute arbitrary JavaScript in the scene process.
|
||||
* `cc` is available in scope; the value of the last expression is returned
|
||||
* (compatible with `(function(){ ... })();` IIFE scripts).
|
||||
*/
|
||||
executeScript(script: string) {
|
||||
try {
|
||||
const cc = require('cc');
|
||||
const body = String(script).trim().replace(/;+\s*$/, '');
|
||||
let fn: Function;
|
||||
try {
|
||||
fn = new Function('cc', 'return (' + body + '\n);');
|
||||
} catch (_e) {
|
||||
fn = new Function('cc', script);
|
||||
}
|
||||
const result = fn(cc);
|
||||
let data: any = result;
|
||||
try { JSON.stringify(result); } catch (_) { data = String(result); }
|
||||
return { success: true, data: { result: data } };
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new scene
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user