2

I've written a function in JavaScript. For example:

function myName(a)
{
   var k=10;
   print(k);
}

Is it possible to call myName from C using JavaScriptCore? If so, how?

icktoofay
  • 126,289
  • 21
  • 250
  • 231
user1872227
  • 141
  • 3

2 Answers2

0

You may want to have a look at the Google's V8 project (It's written in C++, though). Which allows you to compile and execute a javascript program within a C++ program (with the help of the embeded v8 engine). Here is its Getting Started document page

Lei Mou
  • 2,562
  • 1
  • 21
  • 29
  • Basically I'm accessing my all the c functions in javascript by using the SWIG Tool(It creates wrapper functions corresponding to all API's used). You can refer link for more description about my goal. http://stackoverflow.com/questions/13681780/javascript-extension-to-use-c-based-apisclutter-in-a-webapp All my API's has been working fine after exposing it. One single API "g_signal_connect" [http://developer.gnome.org/gobject/unstable/gobject-Signals.html#g-signal-connect] I'm not able to expose. Please assist If you have any idea about calling this. – user1872227 Dec 20 '12 at 10:14
  • My Wrapper function is like this: ulong JS_g_signal_connect(ClutterActor* actor,const char* eventName, char* calledFunction, gpointer data){ return g_signal_connect(actor, eventName, GCallback(calledFunction), data); } My function "calledFunction" is defined in javascript. I'm calling my wrapper function from javascript language. with the syntax example.JS_g_signal_connect(actor, "button-press-event,"functionName", NULL); – user1872227 Dec 20 '12 at 10:31
  • I'm not familiar with SWIG, but it seems quite interesting. I'll take a look at my spare time. I'll leave a post if I can do someting :-) – Lei Mou Dec 20 '12 at 10:34
0

Yes, you can. One way to do it is, from C, call JSEvaluateScript. Something like this:

JSStringRef script = JSStringCreateWithUTF8CString("myName()");
JSEvaluateScript(ctx, script, NULL, NULL, 0, NULL);

This assumes you've created ctx (a JSContextRef) using JSGlobalContextCreate and have already evaluated your script in that context (again using JSEvaluateScript) to add the myName function to that ctx.

Doug Richardson
  • 10,483
  • 6
  • 51
  • 77