calling javascript function from C

Posted on
  • I would like to call a javascript function from c and use the return value.
    In first test:

    /*JSON{
      "type" : "function",
      "name" : "ExecJS",
      "generate" : "jswrap_ExecJS",
      "params" : [
        ["function","JsVar","A Function or String to be executed"]
      ],
      "return" : ["JsVar","can be used for everything"]
    }
    */
    JsVar *jswrap_ExecJS(JsVar *func){
      JsVar x = *jspExecuteFunction(func, 0,0,0);
      jsWarn("na sowas: %d\n",x);
      jsvUnLock(&x);
      jsWarn("done\n");
    }
    

    got this error, after ExecJS(function(){return 99;}); whats wrong ?

    WARNING: na sowas: 99
    WARNING: done
    =ASSERT(v->flags == JSV_UNUSED) FAILED AT src/jsvar.c:399
    #1[r1,l2] Object {

    #2[r1,l2] Name String [1 blocks] "\xFF"      #3[r1,l2] Object { 
        #6[r1,l2] Name String [2 blocks] "timers"          #8[r2,l1] Array(0) [ ]
        #9[r1,l2] Name String [2 blocks] "watches"          #11[r2,l1] Array(0) [ ]
        #13[r1,l2] Name String [1 blocks] "net"          #14[r1,l2] String [1 blocks] "\x05\x00\x00\x00\x00\xFF\xFF\xFF"
        #15[r1,l2] Name String [2 blocks] "history"          #25[r1,l1] Array(2) [
            #26[r1,l2] Name Integer 0              #19[r1,l1] String [5 blocks] "console.log(\"<\",\"<<\",JSON.stringify­(process.env),\">>\",\">\")"
            #224[r1,l2] Name Integer 1              #213[r1,l1] String [3 blocks] "ExecJS(function(){return 99;});"
          ]
      } 
    #17[r1,l2] Name String [2 blocks] "console"      #18[r1,l1] NativeFunction 0x40123264 (1) { }
    #28[r1,l2] Name String [1 blocks] "JSON"      #27[r1,l1] NativeFunction 0x401232ac (1) { }
    #33[r1,l2] Name String [2 blocks] "process"      #32[r1,l1] NativeFunction 0x40123210 (1) { }
    

    }
    HALTING.

  • The issue isn't executing a function - it's just that the function you have defined in C isn't returning a value (so it'll be returning some random memory address).

    If you stick a return 0 at the end of it, it should work. It's worth checking out the warnings the compiler gives you, as it probably would have flagged that one up :)

  • Business as usual, problems between ears of developer ;-)
    Thanks a lot

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

calling javascript function from C

Posted by Avatar for JumJum @JumJum

Actions