Comments (5)
please refer README.md
const vLog = vm.createFunction('log', function() {
const temp = [];
for(let i = 0; i < arguments.length; i++){
temp.push(vm.asString(arguments[i]));
}
console.log(...temp); // '1', 1, false
});
vm.call(
vLog, // function
vm.createUndefined(), // this ptr
vm.createString('1'), // arguments[0]
vm.createNumber(1), // arguments[1]
vm.createBoolean(false) // arguments[2]
);
from sablejs.
Thanks for your reply!
VM.call instructions in the readme:
VM.prototype.call(func, thisPtr[, arg1, arg2, arg3...])
func: Value
thisPtr: Value | undefined
arg: Value
return Value
I try to add "vm.createUndefined()" as second parameter to the call function, but the result returned after execution is undefined:
const vFunc = vm.getProperty(vGlobal, 'sign'); // ****** sign is compiled in vm *****
var vFunc_Ret=vm.call(vFunc,vm.createUndefined(), vm.createString("Hello World!"));
const vFuncValue = vm.asString(vFunc_Ret);
console.log("return:",vFuncValue);
I read the readme and other issues repeatedly, but I still haven't figured out the rules!
from sablejs.
@jimzou A example like this:
// sign.js
function sign(data) {
return data + ", do what you want";
}
Then you should run below shell script and get output result
> sablejs sign.js
Finally, you can call it and get the return value:
// nodejs 16.15.1
const VM = require("sablejs/runtime")();
const fs = require("fs");
const vm = new VM();
// please run: sablejs -i fib.js -o output
vm.run(fs.readFileSync("./output").toString());
const vGlobal = vm.getGlobal();
const vSign = vm.getProperty(vGlobal, "sign");
const vRet = vm.call(vSign, vGlobal, vm.createString("Hello World"));
console.log(vm.asString(vRet)); // output: Hello World, do what you want
vm.destroy();
or
<!--chrome latest-->
<html>
<body>
<script src="../runtime.js"></script>
<script>
var vm = new (sablejs())();
const vGlobal = vm.getGlobal();
(async () => {
const resp = await fetch("./output");
const data = await resp.text();
vm.run(data);
const vFunc = vm.getProperty(vGlobal, "sign");
const vFunc_Ret = vm.call(vFunc, vGlobal, vm.createString("Hello World!"));
const vFuncValue = vm.asString(vFunc_Ret);
console.log("return:", vFuncValue);
vm.destroy();
})();
</script>
</body>
</html>
It was passed by node 16.15.1 / sablejs 1.1.0 / chrome latest.
from sablejs.
If have any other problem, please reopen it, thx.
from sablejs.
solved. Thanks for your regular work!
from sablejs.
Related Issues (20)
- 有关浏览器环境配置 HOT 5
- 从虚拟机内部能否读取操作页面DOM元素? HOT 1
- VM中能否支持SM4加密算法,实际操作中发现会抛异常。 HOT 4
- sablejs -v 报错: SyntaxError: Unexpected token . HOT 1
- > @panchaowu HOT 2
- @zdrin If you want to new EventDispatcher, you should bind cax to sablejs, like cax4sablejs doc said: HOT 4
- 在Node环境下运行cli提示`import` 已经被占用 HOT 1
- 能否支持commonjs/umd的模块导出 HOT 1
- Could you open up the source code? HOT 1
- 运行报错 (win10) Download.js:265 HOT 1
- Compiler source code / Redistribute MacOS compiler binary HOT 2
- 在react下使用sablejs 卡顿 HOT 7
- macbook pro m1 error HOT 1
- HI, could you help me to solve this problem,thx! HOT 4
- is sablejs2.0 still wip or not maintained any more? HOT 4
- the source code of sablejs compiler executable binary file is going to be opened? HOT 1
- Uncaught (in promise) ReferenceError: 'require' is not defined
- 提示 compile failed: SyntaxError: Parenthesized pattern! HOT 1
- windows11 sablejs -i fib.js -o ouput 结束无输出 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sablejs.