Giter Site home page Giter Site logo

Doubt about how to implement my code about mujs HOT 4 CLOSED

ccxvii avatar ccxvii commented on September 28, 2024
Doubt about how to implement my code

from mujs.

Comments (4)

ccxvii avatar ccxvii commented on September 28, 2024 1

A few misconceptions that need to be cleared up.

js_loadstring pushes an anonymous function that when run executes the code in the string. In your example, the function created by js_loadstring will define another function called myFunction when it is run.

The "this" value you need to push when calling a function is the value that 'this' will be bound to when executing the function. If you don't know or understand what this means, just use js_pushundefined.

js_newstate(NULL, NULL, JS_STRICT);
js_loadstring(J, "myfun", "function myFunction(p1, p2) { return p1+p2; }");
js_pushundefined(J); // we don't have a 'this' object
js_call(J, 0); // execute the script function that defines myFunction

Now we have run the code that defines myFunction. Now you can use it, like so:

js_getglobal(J, "myFunction"); // get the myFunction object
js_pushundefined(J;) // no this binding here either
js_pushnumber(J, 2); // argument 1
js_pushnumber(J, 3); // argument 2
js_call(J, 2); // call with 2 arguments
ret = js_tonumber(J, -1); // read return value
js_pop(J, 1); // pop return value to clean up stack

from mujs.

avih avatar avih commented on September 28, 2024

js_loadfile gets a file name and compiles (parses) the file's content - it does not yet run it.

js_loadstring is the same, but you're providing the content of the file as the other argument. I.e. the vm thinks it loads a file with some name (for stack traces), but it never actually touches the disk, and takes your string as its content instead.

On both cases, after parsing, you need to also run it to actually apply the code (which in your case will end up with a global function myFunction).

So right after your js_loadstring, you need to "call" the parsed content to also run it, and you call it like you call anything else in mujs - push a this value, e.g. js_pushglobal(J); and js_call(J, 0); (no arguments).

The rest of your code should now work.

Alternatively, you can shorten this triplet (parse content, push this, call it to run it) to one command: js_dostring(J, <content-string>), which you can use instead of your js_loadstring, and it should work.

from mujs.

AsierRF avatar AsierRF commented on September 28, 2024

First of all, thanks for your help.

The code right now prints "Nan" as result.

I am afraid I did not understand how I can do this that you mention:

So right after your js_loadstring, you need to "call" the parsed content to also run it, and you call it like you call anything else in mujs

Based on the reference page, the function js_loadstring already pushes the function after compiling. And I can't find any function in the reference page that is used to call the functions. Could you please right the required functions structure?

An extra question related to js_dostring. Can I also set the input variables and get the return values if I use js_dostring?

from mujs.

AsierRF avatar AsierRF commented on September 28, 2024

Thanks a lot for your help.
The code works fine and now I understand the process.
I would suggest you add this or a similar example to the "Examples" page. I think it would help people understand it better.

from mujs.

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.