<div dir="ltr"><div class="gmail_quote"><div dir="ltr">Hi,<div><br></div><div>Sorry if I'm writing to a wrong list. I'm not a subscriber, just got a question I'd like to clarify with JavaScriptCore gurus.</div>
<div><br></div><div>I'm writing a JS binding for an existing class hierarchy and I'm trying to set up a working inheritance model that would allow to extend classes exported from C++ in JavaScript.</div>
<div><br></div><div>I have a problem with shadowing a prototype's readonly property. Here's a minimal code sample:</div><div><br></div><div><div>JSStaticFunction sceneFuns[] = {</div><div> { "ctor", Scene_ctor, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },</div>
<div> {0, 0, 0}</div><div>};</div><div><br></div><div>// I don't think nodeClass is relevant here, but I left it in to stay as close</div><div>// to the original code as possible</div><div>JSClassDefinition classdef = {</div>
<div> .version = 0,</div><div> .attributes = kJSClassAttributeNone,</div><div> .className = "Scene",</div><div> .parentClass = nodeClass.jsclass,</div><div> .staticValues = NULL,</div><div> .staticFunctions = sceneFuns,</div>
<div> .callAsConstructor = NULL,</div><div>};</div><div>JSClassRef jsclass = JSClassCreate(&classdef);</div><div>JSObjectRef sceneClassObj = JSObjectMake(ctx, jsclass, NULL);</div><div><br></div><div>//</div><div>
// Then, inside a callback called from JavaScript as "this.newPrototype()":</div>
<div>//</div><div><br></div>
<div>JSValueRef jsCallback(...)</div><div>{</div><div> // The class is intentionally left NULL here. We don't want to create a separate JSClassRef</div><div> // for every subclass created in JavaScript code</div>
<div> JSObjectRef newClassObj = JSObjectMake(ctx, NULL, NULL);</div><div><br></div><div> JSObjectSetPrototype(ctx, newClassObj, sceneClassObj); // <--- offending line</div><div> return newClassObj;</div><div>
}</div><div><br></div><div>After executing JS code like this "var proto = this.newPrototype(); proto.ctor = ...", the value of "ctor" does not change. If I comment out the offending line (marked in the code above), it becomes possible to override it.</div>
<div><br></div><div>This behaviour is surprising me. To my knowledge, an object's prototype is consulted when a property is not found on the object itself. Any new property is written to the object, never its prototype. So I was expecting to add the "ctor" property on "newClassObj" directly and shadow the prototype's read-only "ctor", but it seems that either the property is set on the object's prototype (and the ReadOnly attribute makes it a no-op) or the ReadOnly attribute contaminates the object itself.</div>
<div><br></div><div>Perhaps I'm misunderstanding the role of prototypes in JavaScriptCore. Could anyone help clear out the confusion for me?</div><div><br></div><div>Thanks!</div></div></div></div><div><br></div>-- <br>
Best regards<br>Alexei Sholik
</div>