Giter VIP home page Giter VIP logo

Comments (5)

ErosZy avatar ErosZy commented on May 19, 2024

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.

jimzou avatar jimzou commented on May 19, 2024

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.

ErosZy avatar ErosZy commented on May 19, 2024

@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.

ErosZy avatar ErosZy commented on May 19, 2024

If have any other problem, please reopen it, thx.

from sablejs.

jimzou avatar jimzou commented on May 19, 2024

solved. Thanks for your regular work!

from sablejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.