<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[180467] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/180467">180467</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2015-02-20 16:36:27 -0800 (Fri, 20 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Refactor JSWrapperMap.mm to defer creation of the ObjC JSValue until the latest possible moment.
&lt;https://webkit.org/b/141856&gt;

Reviewed by Geoffrey Garen.

1. Make JSObjCClassInfo's -constructor and -wrapperForObject return a
   JSC::JSObject* just like -prototype.
2. Defer the creation of the ObjC JSValue from JSC::JSObject* until
   the latest moment when it is needed.  This allows us to not have to
   keep converting back to a JSC::JSObject* in intermediate code.

* API/JSWrapperMap.mm:
(makeWrapper):
(objectWithCustomBrand):
(constructorWithCustomBrand):
(allocateConstructorForCustomClass):
(-[JSObjCClassInfo allocateConstructorAndPrototype]):
(-[JSObjCClassInfo wrapperForObject:]):
(-[JSObjCClassInfo constructor]):
(-[JSWrapperMap jsWrapperForObject:]):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreAPIJSWrapperMapmm">trunk/Source/JavaScriptCore/API/JSWrapperMap.mm</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreAPIJSWrapperMapmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/API/JSWrapperMap.mm (180466 => 180467)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/API/JSWrapperMap.mm        2015-02-21 00:35:18 UTC (rev 180466)
+++ trunk/Source/JavaScriptCore/API/JSWrapperMap.mm        2015-02-21 00:36:27 UTC (rev 180467)
</span><span class="lines">@@ -108,7 +108,7 @@
</span><span class="cx">     return JSC::JSObject::defaultHasInstance(exec, instance, constructor-&gt;get(exec, exec-&gt;propertyNames().prototype));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static JSObjectRef makeWrapper(JSContextRef ctx, JSClassRef jsClass, id wrappedObject)
</del><ins>+static JSC::JSObject* makeWrapper(JSContextRef ctx, JSClassRef jsClass, id wrappedObject)
</ins><span class="cx"> {
</span><span class="cx">     JSC::ExecState* exec = toJS(ctx);
</span><span class="cx">     JSC::JSLockHolder locker(exec);
</span><span class="lines">@@ -119,33 +119,33 @@
</span><span class="cx">     if (JSC::JSObject* prototype = jsClass-&gt;prototype(exec))
</span><span class="cx">         object-&gt;setPrototype(exec-&gt;vm(), prototype);
</span><span class="cx"> 
</span><del>-    return toRef(object);
</del><ins>+    return object;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> // Make an object that is in all ways a completely vanilla JavaScript object,
</span><span class="cx"> // other than that it has a native brand set that will be displayed by the default
</span><span class="cx"> // Object.prototype.toString conversion.
</span><del>-static JSValue *objectWithCustomBrand(JSContext *context, NSString *brand, Class cls = 0)
</del><ins>+static JSC::JSObject *objectWithCustomBrand(JSContext *context, NSString *brand, Class cls = 0)
</ins><span class="cx"> {
</span><span class="cx">     JSClassDefinition definition;
</span><span class="cx">     definition = kJSClassDefinitionEmpty;
</span><span class="cx">     definition.className = [brand UTF8String];
</span><span class="cx">     JSClassRef classRef = JSClassCreate(&amp;definition);
</span><del>-    JSObjectRef result = makeWrapper([context JSGlobalContextRef], classRef, cls);
</del><ins>+    JSC::JSObject* result = makeWrapper([context JSGlobalContextRef], classRef, cls);
</ins><span class="cx">     JSClassRelease(classRef);
</span><del>-    return [JSValue valueWithJSValueRef:result inContext:context];
</del><ins>+    return result;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-static JSValue *constructorWithCustomBrand(JSContext *context, NSString *brand, Class cls)
</del><ins>+static JSC::JSObject *constructorWithCustomBrand(JSContext *context, NSString *brand, Class cls)
</ins><span class="cx"> {
</span><span class="cx">     JSClassDefinition definition;
</span><span class="cx">     definition = kJSClassDefinitionEmpty;
</span><span class="cx">     definition.className = [brand UTF8String];
</span><span class="cx">     definition.hasInstance = constructorHasInstance;
</span><span class="cx">     JSClassRef classRef = JSClassCreate(&amp;definition);
</span><del>-    JSObjectRef result = makeWrapper([context JSGlobalContextRef], classRef, cls);
</del><ins>+    JSC::JSObject* result = makeWrapper([context JSGlobalContextRef], classRef, cls);
</ins><span class="cx">     JSClassRelease(classRef);
</span><del>-    return [JSValue valueWithJSValueRef:result inContext:context];
</del><ins>+    return result;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> // Look for @optional properties in the prototype containing a selector to property
</span><span class="lines">@@ -365,8 +365,8 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (id)initWithContext:(JSContext *)context forClass:(Class)cls;
</span><del>-- (JSValue *)wrapperForObject:(id)object;
-- (JSValue *)constructor;
</del><ins>+- (JSC::JSObject *)wrapperForObject:(id)object;
+- (JSC::JSObject *)constructor;
</ins><span class="cx"> - (JSC::JSObject *)prototype;
</span><span class="cx"> 
</span><span class="cx"> @end
</span><span class="lines">@@ -397,7 +397,7 @@
</span><span class="cx">     [super dealloc];
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static JSValue *allocateConstructorForCustomClass(JSContext *context, const char* className, Class cls)
</del><ins>+static JSC::JSObject* allocateConstructorForCustomClass(JSContext *context, const char* className, Class cls)
</ins><span class="cx"> {
</span><span class="cx">     if (!supportsInitMethodConstructors())
</span><span class="cx">         return constructorWithCustomBrand(context, [NSString stringWithFormat:@&quot;%sConstructor&quot;, className], cls);
</span><span class="lines">@@ -444,7 +444,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         JSObjectRef method = objCCallbackFunctionForInit(context, cls, initProtocol, initMethod, types);
</span><del>-        return [JSValue valueWithJSValueRef:method inContext:context];
</del><ins>+        return toJS(method);
</ins><span class="cx">     }
</span><span class="cx">     return constructorWithCustomBrand(context, [NSString stringWithFormat:@&quot;%sConstructor&quot;, className], cls);
</span><span class="cx"> }
</span><span class="lines">@@ -457,36 +457,32 @@
</span><span class="cx"> 
</span><span class="cx">     ASSERT(!m_constructor || !m_prototype);
</span><span class="cx">     ASSERT((m_class == [NSObject class]) == !superClassInfo);
</span><ins>+
+    JSC::JSObject* jsPrototype = m_prototype.get();
+    JSC::JSObject* jsConstructor = m_constructor.get();
+
</ins><span class="cx">     if (!superClassInfo) {
</span><span class="cx">         JSContextRef cContext = [m_context JSGlobalContextRef];
</span><span class="cx">         JSValue *constructor = m_context[@&quot;Object&quot;];
</span><del>-        if (!m_constructor)
-            m_constructor = toJS(JSValueToObject(cContext, valueInternalValue(constructor), 0));
</del><ins>+        if (!jsConstructor)
+            jsConstructor = toJS(JSValueToObject(cContext, valueInternalValue(constructor), 0));
</ins><span class="cx"> 
</span><del>-        if (!m_prototype) {
</del><ins>+        if (!jsPrototype) {
</ins><span class="cx">             JSValue *prototype = constructor[@&quot;prototype&quot;];
</span><del>-            m_prototype = toJS(JSValueToObject(cContext, valueInternalValue(prototype), 0));
</del><ins>+            jsPrototype = toJS(JSValueToObject(cContext, valueInternalValue(prototype), 0));
</ins><span class="cx">         }
</span><span class="cx">     } else {
</span><span class="cx">         const char* className = class_getName(m_class);
</span><span class="cx"> 
</span><span class="cx">         // Create or grab the prototype/constructor pair.
</span><del>-        JSValue *prototype;
-        JSValue *constructor;
-        if (m_prototype)
-            prototype = [JSValue valueWithJSValueRef:toRef(m_prototype.get()) inContext:m_context];
-        else
-            prototype = objectWithCustomBrand(m_context, [NSString stringWithFormat:@&quot;%sPrototype&quot;, className]);
</del><ins>+        if (!jsPrototype)
+            jsPrototype = objectWithCustomBrand(m_context, [NSString stringWithFormat:@&quot;%sPrototype&quot;, className]);
</ins><span class="cx"> 
</span><del>-        if (m_constructor)
-            constructor = [JSValue valueWithJSValueRef:toRef(m_constructor.get()) inContext:m_context];
-        else
-            constructor = allocateConstructorForCustomClass(m_context, className, m_class);
</del><ins>+        if (!jsConstructor)
+            jsConstructor = allocateConstructorForCustomClass(m_context, className, m_class);
</ins><span class="cx"> 
</span><del>-        JSContextRef cContext = [m_context JSGlobalContextRef];
-        m_prototype = toJS(JSValueToObject(cContext, valueInternalValue(prototype), 0));
-        m_constructor = toJS(JSValueToObject(cContext, valueInternalValue(constructor), 0));
-
</del><ins>+        JSValue* prototype = [JSValue valueWithJSValueRef:toRef(jsPrototype) inContext:m_context];
+        JSValue* constructor = [JSValue valueWithJSValueRef:toRef(jsConstructor) inContext:m_context];
</ins><span class="cx">         putNonEnumerable(prototype, @&quot;constructor&quot;, constructor);
</span><span class="cx">         putNonEnumerable(constructor, @&quot;prototype&quot;, prototype);
</span><span class="cx"> 
</span><span class="lines">@@ -498,12 +494,15 @@
</span><span class="cx"> 
</span><span class="cx">         // Set [Prototype].
</span><span class="cx">         JSC::JSObject* superClassPrototype = [superClassInfo prototype];
</span><del>-        JSObjectSetPrototype([m_context JSGlobalContextRef], toRef(m_prototype.get()), toRef(superClassPrototype));
</del><ins>+        JSObjectSetPrototype([m_context JSGlobalContextRef], toRef(jsPrototype), toRef(superClassPrototype));
</ins><span class="cx">     }
</span><del>-    return ConstructorPrototypePair(m_constructor.get(), m_prototype.get());
</del><ins>+
+    m_prototype = jsPrototype;
+    m_constructor = jsConstructor;
+    return ConstructorPrototypePair(jsConstructor, jsPrototype);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-- (JSValue *)wrapperForObject:(id)object
</del><ins>+- (JSC::JSObject*)wrapperForObject:(id)object
</ins><span class="cx"> {
</span><span class="cx">     ASSERT([object isKindOfClass:m_class]);
</span><span class="cx">     ASSERT(m_block == [object isKindOfClass:getNSBlockClass()]);
</span><span class="lines">@@ -513,24 +512,24 @@
</span><span class="cx">             JSValue *prototype = [JSValue valueWithNewObjectInContext:m_context];
</span><span class="cx">             putNonEnumerable(constructor, @&quot;prototype&quot;, prototype);
</span><span class="cx">             putNonEnumerable(prototype, @&quot;constructor&quot;, constructor);
</span><del>-            return constructor;
</del><ins>+            return toJS(method);
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     JSC::JSObject* prototype = [self prototype];
</span><span class="cx"> 
</span><del>-    JSObjectRef wrapper = makeWrapper([m_context JSGlobalContextRef], m_classRef, object);
-    JSObjectSetPrototype([m_context JSGlobalContextRef], wrapper, toRef(prototype));
-    return [JSValue valueWithJSValueRef:wrapper inContext:m_context];
</del><ins>+    JSC::JSObject* wrapper = makeWrapper([m_context JSGlobalContextRef], m_classRef, object);
+    JSObjectSetPrototype([m_context JSGlobalContextRef], toRef(wrapper), toRef(prototype));
+    return wrapper;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-- (JSValue *)constructor
</del><ins>+- (JSC::JSObject*)constructor
</ins><span class="cx"> {
</span><span class="cx">     JSC::JSObject* constructor = m_constructor.get();
</span><span class="cx">     if (!constructor)
</span><span class="cx">         constructor = [self allocateConstructorAndPrototype].first;
</span><span class="cx">     ASSERT(!!constructor);
</span><del>-    return [JSValue valueWithJSValueRef:toRef(constructor) inContext:m_context];
</del><ins>+    return constructor;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (JSC::JSObject*)prototype
</span><span class="lines">@@ -595,12 +594,11 @@
</span><span class="cx">     if (jsWrapper)
</span><span class="cx">         return [JSValue valueWithJSValueRef:toRef(jsWrapper) inContext:m_context];
</span><span class="cx"> 
</span><del>-    JSValue *wrapper;
</del><span class="cx">     if (class_isMetaClass(object_getClass(object)))
</span><del>-        wrapper = [[self classInfoForClass:(Class)object] constructor];
</del><ins>+        jsWrapper = [[self classInfoForClass:(Class)object] constructor];
</ins><span class="cx">     else {
</span><span class="cx">         JSObjCClassInfo* classInfo = [self classInfoForClass:[object class]];
</span><del>-        wrapper = [classInfo wrapperForObject:object];
</del><ins>+        jsWrapper = [classInfo wrapperForObject:object];
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=105891
</span><span class="lines">@@ -608,10 +606,8 @@
</span><span class="cx">     // (1) For immortal objects JSValues will effectively leak and this results in error output being logged - we should avoid adding associated objects to immortal objects.
</span><span class="cx">     // (2) A long lived object may rack up many JSValues. When the contexts are released these will unprotect the associated JavaScript objects,
</span><span class="cx">     //     but still, would probably nicer if we made it so that only one associated object was required, broadcasting object dealloc.
</span><del>-    JSC::ExecState* exec = toJS([m_context JSGlobalContextRef]);
-    jsWrapper = toJS(exec, valueInternalValue(wrapper)).toObject(exec);
</del><span class="cx">     m_cachedJSWrappers.set(object, jsWrapper);
</span><del>-    return wrapper;
</del><ins>+    return [JSValue valueWithJSValueRef:toRef(jsWrapper) inContext:m_context];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (JSValue *)objcWrapperForJSValueRef:(JSValueRef)value
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (180466 => 180467)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-02-21 00:35:18 UTC (rev 180466)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-02-21 00:36:27 UTC (rev 180467)
</span><span class="lines">@@ -1,3 +1,26 @@
</span><ins>+2015-02-20  Mark Lam  &lt;mark.lam@apple.com&gt;
+
+        Refactor JSWrapperMap.mm to defer creation of the ObjC JSValue until the latest possible moment.
+        &lt;https://webkit.org/b/141856&gt;
+
+        Reviewed by Geoffrey Garen.
+
+        1. Make JSObjCClassInfo's -constructor and -wrapperForObject return a
+           JSC::JSObject* just like -prototype.
+        2. Defer the creation of the ObjC JSValue from JSC::JSObject* until
+           the latest moment when it is needed.  This allows us to not have to
+           keep converting back to a JSC::JSObject* in intermediate code.
+
+        * API/JSWrapperMap.mm:
+        (makeWrapper):
+        (objectWithCustomBrand):
+        (constructorWithCustomBrand):
+        (allocateConstructorForCustomClass):
+        (-[JSObjCClassInfo allocateConstructorAndPrototype]):
+        (-[JSObjCClassInfo wrapperForObject:]):
+        (-[JSObjCClassInfo constructor]):
+        (-[JSWrapperMap jsWrapperForObject:]):
+
</ins><span class="cx"> 2015-02-20  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Build fix for gcc.
</span></span></pre>
</div>
</div>

</body>
</html>