Subj : Re: Create Vars from C To : netscape.public.mozilla.jseng From : Jamie Nordmeyer Date : Fri Mar 25 2005 07:50 am This is a multi-part message in MIME format. ------=_NextPart_000_0025_01C5310F.52ED6FD0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi Frank. You have to call JS_InitClass, as always, to initialize your = class, then you call JS_DefineObject, passing in your global object as = the parent. JS_DefineObject adds a class to another as a property. Now = you can call "obj.doSomething();" to your hearts content. Here's the = Init method for a project I'm working on that allows me to do exactly = what you're asking: JSObject* jsDirectories::proto =3D NULL; JSBool jsDirectories::Init(JSContext* cx, JSObject* obj) { if (!(proto =3D JS_InitClass(cx, obj, NULL, &Directories_class, Constructor, 0, properties, NULL, NULL, NULL))) return JS_FALSE; JS_DefineObject(cx, obj, "Directories", &Directories_class, proto, = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); return JS_TRUE; } Hope that helps. "Frank Luctos" wrote in message = news:d215pg$av41@ripley.netscape.com... > Thanks, Todd. >=20 > I understand creating custom objects and have been doing that for a = while.=20 > What I've never had occasion to do is to have an instance of an object = > created automatically, e.g. without the script author having to = execure a=20 > "new" statement. >=20 > Can anyone tell me how to do the equiv. of a "var x =3D new obj" from = within=20 > my C code? >=20 > thanks again, >=20 > Frank >=20 >=20 > "Todd Fisher" wrote in message=20 > news:mailman.1111716180.4452.mozilla-jseng@mozilla.org... > > Hi Frank, > > I haven't tried it yet but I did read through and thought this = document=20 > > was very informative. So, I thought you might also find it helpful = :-) > > http://users.skynet.be/saw/SpiderMonkey.htm > > > > -Todd > > > > Frank Luctos wrote: > > > >>Hi, > >> > >>Can anyone tell me how I can create an object in my C code that is = visible=20 > >>inside a JavaScript? > >> > >>To explain, lets say I want a variable called "MyVar" inside my = JavaScript=20 > >>that points to an object I've created. > >> > >>What SpiderMonkey API's do I have to execute in the C code to make = that=20 > >>MyVar available inside a particular script? > >> > >>Thanks in advance, > >> > >>Frank > >> > >>_______________________________________________ > >>mozilla-jseng mailing list > >>mozilla-jseng@mozilla.org > >>http://mail.mozilla.org/listinfo/mozilla-jseng > >> > > > >=20 >=20 > ------=_NextPart_000_0025_01C5310F.52ED6FD0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi Frank.  You have to call = JS_InitClass, as=20 always, to initialize your class, then you call JS_DefineObject, passing = in your=20 global object as the parent.  JS_DefineObject adds a class to = another as a=20 property.  Now you can call "obj.doSomething();" to your hearts=20 content.  Here's the Init method for a project I'm working on that = allows=20 me to do exactly what you're asking:
 
JSObject* jsDirectories::proto =3D = NULL;
 
JSBool jsDirectories::Init(JSContext* = cx, JSObject*=20 obj)
{
    if=20 (!(proto =3D JS_InitClass(cx, obj, NULL,
       =20 &Directories_class, Constructor, 0, properties,
        = NULL, NULL,=20 NULL)))
        = return JS_FALSE;
    JS_DefineObject(cx, = obj,=20 "Directories", &Directories_class, proto, JSPROP_ENUMERATE | = JSPROP_READONLY=20 | JSPROP_PERMANENT);
    return JS_TRUE;
}
 
Hope that helps.
 
"Frank Luctos" <flucto@yahoo.com> = wrote in message=20 news:d215pg$av41@ripley.netscape.com...
> Thanks, = Todd.
>=20
> I understand creating custom objects and have been doing that = for a=20 while.
> What I've never had occasion to do is to have an = instance of an=20 object
> created automatically, e.g. without the script author = having to=20 execure a
> "new" statement.
>
> Can anyone tell me = how to=20 do the equiv. of a "var x =3D new obj" from within
> my C = code?
>=20
> thanks again,
>
> Frank
>
>
> = "Todd=20 Fisher" <
toddf@simosoftware.com> wrote in=20 message
>
news:mailman.1111716180.4452.mozilla-jseng@mozilla.org...
> > Hi Frank,
> = >    I=20 haven't tried it yet but I did read through and thought this document =
>=20 > was very informative.  So, I thought you might also find it = helpful=20 :-)
> > http://users.skynet.be/saw/SpiderMonkey.htm
> >
> > -Todd
> >
> = > Frank=20 Luctos wrote:
> >
> >>Hi,
> >>
>=20 >>Can anyone tell me how I can create an object in my C code that = is=20 visible
> >>inside a JavaScript?
> >>
>=20 >>To explain, lets say I want a variable called "MyVar" inside my=20 JavaScript
> >>that points to an object I've = created.
>=20 >>
> >>What SpiderMonkey API's do I have to execute in = the C=20 code to make that
> >>MyVar available inside a particular=20 script?
> >>
> >>Thanks in advance,
>=20 >>
> >>Frank
> >>
>=20 >>_______________________________________________
>=20 >>mozilla-jseng mailing list
>=20 >>mozilla-jseng@mozilla.org
>=20 >>http://mail.mozilla.org/listinfo/mozilla-jseng
> = >>
>=20 >
> >
>
>
------=_NextPart_000_0025_01C5310F.52ED6FD0-- .