Executing a line of C code in javascript

H

Heath Raftery

Okay, I've been working with SpiderMonkey to execute some javascript in
my C application. Now, I need to execute a line of C code within my
javascript!

I've gone round and round trying to find a solution - I need to be able
to parse PAC (proxy automatic configuration) scripts in my application.
I turned to SpiderMonkey, eventually found the pacUtils in the mozilla
netwerk tree, and got 90% there. Unfortunately, a few of the functions
in pacUtils require a dns lookup, which is not supported in plain
javascript. Mozilla appears to handle it by linking to a c++ class
called nsDNSService.

I noticed that previously in this group, people have claimed that C++/C
functions are tricky to execute in javascript, possibly requiring
ActiveX interfaces or JNI work. However, mozilla seems to do it like
this (code is selected parts of original files):

*In the pacUtils file*
var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);

*In the nsIDNSService.idl file*
%{C++
#define NS_DNSSERVICE_CID {/* 718e7c81-f8b8-11d2-b951-c80918051d3c */
0x718e7c81, 0xf8b8, 0x11d2, { 0xb9, 0x51, 0xc8, 0x09, 0x18, 0x05, 0x1d,
0x3c } }
%}

[scriptable, uuid (598f2f80-206f-11d3-9348-00104ba0fd40)]
interface nsIDNSService : nsISupports
{
string resolve (in string hostname);
}

*In the nsDNSService.h file*
class nsDNSService : blah, blah
{
public:
blah, blah
}

*In the nsDNSService.cpp file*
NS_IMETHODIMP nsDNSService::Resolve(const char *i_hostname, char **o_ip)
{
blah, blah
return *o_ip ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

So, how would I achieve this without including most of mozilla in my
application? Is there a simpler way of achieving the same? I just want
to resolve a host name! Or even simpler - I just to parse a PAC file!
This is turning out to be much more difficult that I expected...

Regards,
Heath

--
Heath
________________________________________________________
| *Nothing is foolproof to a sufficiently talented fool* |
| _\|/_ |
|________________________________________m(. .)m_________|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top