[webkit-help] JavaScriptCore: Is it possible to implement === for your own objects?

Eric Wing ewmailing at gmail.com
Tue Sep 10 13:09:01 PDT 2013


Hello,
I'm pretty new to JavaScriptCore. I hope this is the right place for
questions about it.

I'm trying to figure out the JavaScriptCore API. For my own object
types, is it possible to define my own custom behavior for equality
comparisons (presumably ===)?

I've been looking at JSClassDefinition, specifically "getProperty".
I've also been looking at JSObjectSetPrototype(). For some reason, I
got it in my head (finding small, but incomplete snippets of code via
searches) that these would be what I need to modify to hook into the
equality operator. However, my experiments in actually making these
values do something have yielded nothing, and I have been unable to
find any clear examples doing what I'm trying to do.


So to give you some context about what I'm doing (and my mindset), I
am trying to bind some C libraries to JavaScriptCore for a native app
so I can script stuff in Javascript. I am fairly new to Javascript, so
to help me, I've been using the experimental branch of SWIG to
generate JavaScriptCore bindings. However, the SWIG generator is
incomplete, so I have been forced to get my hands dirty and fix or
implement pieces in the SWIG/JSC generator itself.

The current problem I'm trying to solve is that when a C object
instance crosses into Javascript, JSObjectMake() +
JSObjectSetPrivate(jsobject, c_object) is always used to associate the
Javascript object instance with the C object instance. The problem
arises when the same C instance gets pushed into Javascript from
multiple places. JSObjectMake() creates a distint Javascript object
even though the underlying C pointer is identical.

So in Javascript,
var object1 = Foo.CreateObject();
var object2 = Foo.ReturnSameObject(object1);

(object1 == object2) or (object1 === object2) always return false,
even though I really want them to return true.

So hence, my question about customizing === for my objects. (If
possible, my intention is to add this support to the SWIG generator.)


But this raises a second question. Is it possible to check to see if a
Javascript proxy object already exists and return (push) that one,
instead of calling JSObjectMake() to create a brand new object? This
would be nice so I could use == instead of ===, and also has the
benefit of not generating as much potential garbage that needs to be
collected. To give a little more context, I have a Lua background and
know its C API pretty well. So I've been looking for mechanisms like
doing push/pop operations on a virtual stack and store/retrieve
private things in the registry (with weak references). So far I'm
finding nothing that resembles this so I'm hoping for some
examples/guidance that set me on the correct path.


Thanks,
Eric
-- 
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/


More information about the webkit-help mailing list