SM creating, populating and freeing objects from the host?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

SM creating, populating and freeing objects from the host?

Brian Dupras
I'm new to SM, and I have some questions about the basics.  I'll post
each question independently.  Being new, a reference to the right place
ot look for answers is very much appreciated.


First up - creating and freeing object instances from the host:

I'd like the host to create various "instances" of AttrObject and
arrays of AttrObjects, where my script defines:
  function AttrObject(){
    this.foo="bar";
  }

  AttrObject.prototype.addAttribute = _AttrObject_addAttribute;

  function _AttrObject_addAttribute(...){
        ...
  }


Q1. How do I call the script function AttrObject() as a constructor
from the host?  JS_CallFunctionName doesn't seem to be the equivilent
of "new AttrObject()" and JS_ConstructObject seems to only create
objects defined by the host.


Q2. When the host is done with the object instance, how does the host
properly dispose of the instance?  I either want to "delete" it, or
queue it up for GC, but I'm not seeing how to do this.  How does SM
know that I'm done with the objects so it can GC them?


Brian

_______________________________________________
mozilla-jseng mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-jseng
Reply | Threaded
Open this post in threaded view
|

Re: SM creating, populating and freeing objects from the host?

Brian Dupras
To perhaps answer my own question 2 above -

If I use JS_NewObject or JS_ConstructObject to create an object
instance, then I need to call JS_AddRoot immediately to protect it from
the GC, and JS_RemoveRoot when I'm done with it to allow the GC to
clean it up.  Is this correct?

I'm still curious how I can "new up" an object that's defined by script
rather than native code, if it's possible at all.

_______________________________________________
mozilla-jseng mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-jseng
Reply | Threaded
Open this post in threaded view
|

Re: SM creating, populating and freeing objects from the host?

Brendan Eich
Brian Dupras wrote:
> If I use JS_NewObject or JS_ConstructObject to create an object
> instance, then I need to call JS_AddRoot immediately to protect it from
> the GC, and JS_RemoveRoot when I'm done with it to allow the GC to
> clean it up.  Is this correct?


You mean pass the pointer by its address to JS_AddRoot and JS_RemoveRoot
per the Mozilla Developer Center docs
(http://developer.mozilla.org/en/docs/JS_AddRoot etc.), right?

Also see http://www.mozilla.org/js/spidermonkey/gctips.html.  Note also
that if you *immediately* pass the new object (tagged as a jsval using
OBJECT_TO_JSVAL) to JS_DefineProperty or JS_SetProperty, you don't need
to root -- the newborn root in cx for last-born object protects it.


> I'm still curious how I can "new up" an object that's defined by script
> rather than native code, if it's possible at all.


JS_EvaluateScript is one way.  Another is to call JS_NewObject to make a
blank object (of class Object), then pass it as the |obj| parameter to
JS_CallFunctionName or a kindred API.

/be
_______________________________________________
mozilla-jseng mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-jseng
Reply | Threaded
Open this post in threaded view
|

Re: SM creating, populating and freeing objects from the host?

Brian Dupras
Thanks for the reply -

> You mean pass the pointer by its address to JS_AddRoot and
> JS_RemoveRoot ..., right?

Right.


> Note also
> that if you *immediately* pass the new object (tagged as a jsval using
> OBJECT_TO_JSVAL) to JS_DefineProperty or JS_SetProperty, you don't need
> to root -- the newborn root in cx for last-born object protects it.

I don't understand the "newborn root in cx for last-born object" clause
at the end of that description.  Do you mean, after I've new'd the
object, I can then make something else "point" to the object by setting
the new'd object as a property on another pre-existing object?
Therefore the pre-existnig object would contain a reference to the
new'd object, and that reference would protect the new'd object from GC
at least until the pre-existing object itself gets GC'd.  Correct?

In my case, my intention is to new up the object (from the host), and
populate its properties with a bunch of strings and arrays of strings.
Occassionally the host will pass it as a parameter to a function in the
script, which will return to me a t/f decision.  Eventually the host
will no longer need to call that function with that object and will
want to get rid of the object.

No objects in the script need to have references to my host's new'd
object, so far as I know.  Because of this, I *think* the host should
use Add/RemoveRoot instead of synthesizing a realtionship to another
object.  Does that sound right, or am I just mis-understanding your
description?

_______________________________________________
mozilla-jseng mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-jseng
Reply | Threaded
Open this post in threaded view
|

Re: SM creating, populating and freeing objects from the host?

Brendan Eich
Brian Dupras wrote:

> I don't understand the "newborn root in cx for last-born object" clause
> at the end of that description.  Do you mean, after I've new'd the
> object, I can then make something else "point" to the object by setting
> the new'd object as a property on another pre-existing object?
> Therefore the pre-existnig object would contain a reference to the
> new'd object, and that reference would protect the new'd object from GC
> at least until the pre-existing object itself gets GC'd.  Correct?

Yes, exactly.  But only if you don't call a JS API entry point in
between the JS_New* or JS_Construct* call and the JS_Define* or JS_Set*
call where the intervening call might allocate another object and then
possibly risk running the GC on the calling thread.

You have to know what you are doing with this model.  It is fragile in
general, so we are moving toward a stack-based automatic local root
solution, but not for Mozilla 1.8 / Firefox 1.5 / SpiderMonkey 1.6.


> In my case, my intention is to new up the object (from the host), and
> populate its properties with a bunch of strings and arrays of strings.
> Occassionally the host will pass it as a parameter to a function in the
> script, which will return to me a t/f decision.  Eventually the host
> will no longer need to call that function with that object and will
> want to get rid of the object.


If you are new'ing in a native function, set *rval to the jsval-tagged
new object reference as soon as you can, then store each property value
in it as you go, and you should be fine.  Post or e-mail your code so I
can check.

/be
_______________________________________________
mozilla-jseng mailing list
[hidden email]
http://mail.mozilla.org/listinfo/mozilla-jseng