Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class NativeGenerator { | 7 class NativeGenerator { |
| 8 | |
| 9 static bool needsBoilerplate(JavaScriptBackend backend) { | |
|
sra1
2015/01/05 20:14:23
Like previous CL, I think this needs a better name
floitsch
2015/01/06 12:54:57
Done.
| |
| 10 return backend.needToInitializeIsolateAffinityTag; | |
| 11 } | |
| 12 | |
| 8 /// Generates the boilerplate code necessary for native functions. | 13 /// Generates the boilerplate code necessary for native functions. |
| 9 static jsAst.Statement generateBoilerPlate( | 14 /// |
| 15 /// The [convertToFastObject] argument must point to a JavaScript function | |
| 16 /// which must convert a given argument to a fast object. | |
| 17 static jsAst.Statement generateBoilerplate( | |
| 18 JavaScriptBackend backend, | |
| 10 jsAst.Expression generateEmbeddedGlobalAccess(String global), | 19 jsAst.Expression generateEmbeddedGlobalAccess(String global), |
| 11 {bool initializeIsolateAffinityTag, | 20 jsAst.Expression convertToFastObject) { |
| 12 bool initializeDispatchProperty}) { | 21 assert(backend.needToInitializeIsolateAffinityTag); |
| 13 assert(initializeIsolateAffinityTag != null); | |
| 14 assert(initializeDispatchProperty != null); | |
| 15 assert(!initializeDispatchProperty || initializeIsolateAffinityTag); | |
| 16 | 22 |
| 17 jsAst.Expression getIsolateTagAccess = | 23 jsAst.Expression getIsolateTagAccess = |
| 18 generateEmbeddedGlobalAccess(embeddedNames.GET_ISOLATE_TAG); | 24 generateEmbeddedGlobalAccess(embeddedNames.GET_ISOLATE_TAG); |
| 19 jsAst.Expression isolateTagAccess = | 25 jsAst.Expression isolateTagAccess = |
| 20 generateEmbeddedGlobalAccess(embeddedNames.ISOLATE_TAG); | 26 generateEmbeddedGlobalAccess(embeddedNames.ISOLATE_TAG); |
| 21 jsAst.Expression dispatchPropertyNameAccess = | 27 jsAst.Expression dispatchPropertyNameAccess = |
| 22 generateEmbeddedGlobalAccess(embeddedNames.DISPATCH_PROPERTY_NAME); | 28 generateEmbeddedGlobalAccess(embeddedNames.DISPATCH_PROPERTY_NAME); |
| 23 | 29 |
| 24 return js.statement(''' | 30 return js.statement(''' |
| 25 if (#initializeIsolateAffinityTag) | |
| 26 !function() { | 31 !function() { |
| 27 // On V8, the 'intern' function converts a string to a symbol, which | 32 // On V8, the 'intern' function converts a string to a symbol, which |
| 28 // makes property access much faster. | 33 // makes property access much faster. |
| 29 function intern(s) { | 34 function intern(s) { |
| 30 var o = {}; | 35 var o = {}; |
| 31 o[s] = 1; | 36 o[s] = 1; |
| 32 return Object.keys(convertToFastObject(o))[0]; | 37 return Object.keys(#convertToFastObject(o))[0]; |
| 33 } | 38 } |
| 34 | 39 |
| 35 #getIsolateTag = function(name) { | 40 #getIsolateTag = function(name) { |
| 36 return intern("___dart_" + name + #isolateTag); | 41 return intern("___dart_" + name + #isolateTag); |
| 37 }; | 42 }; |
| 38 | 43 |
| 39 // To ensure that different programs loaded into the same context (page) | 44 // To ensure that different programs loaded into the same context (page) |
| 40 // use distinct dispatch properies, we place an object on `Object` to | 45 // use distinct dispatch properies, we place an object on `Object` to |
| 41 // contain the names already in use. | 46 // contain the names already in use. |
| 42 var tableProperty = "___dart_isolate_tags_"; | 47 var tableProperty = "___dart_isolate_tags_"; |
| 43 var usedProperties = Object[tableProperty] || | 48 var usedProperties = Object[tableProperty] || |
| 44 (Object[tableProperty] = Object.create(null)); | 49 (Object[tableProperty] = Object.create(null)); |
| 45 | 50 |
| 46 var rootProperty = "_${generateIsolateTagRoot()}"; | 51 var rootProperty = "_${generateIsolateTagRoot()}"; |
| 47 for (var i = 0; ; i++) { | 52 for (var i = 0; ; i++) { |
| 48 var property = intern(rootProperty + "_" + i + "_"); | 53 var property = intern(rootProperty + "_" + i + "_"); |
| 49 if (!(property in usedProperties)) { | 54 if (!(property in usedProperties)) { |
| 50 usedProperties[property] = 1; | 55 usedProperties[property] = 1; |
| 51 #isolateTag = property; | 56 #isolateTag = property; |
| 52 break; | 57 break; |
| 53 } | 58 } |
| 54 } | 59 } |
| 55 if (#initializeDispatchProperty) { | 60 if (#initializeDispatchProperty) { |
| 56 #dispatchPropertyName = #getIsolateTag("dispatch_record"); | 61 #dispatchPropertyName = #getIsolateTag("dispatch_record"); |
| 57 } | 62 } |
| 58 }(); | 63 }(); |
| 59 ''', | 64 ''', |
| 60 {'initializeIsolateAffinityTag': initializeIsolateAffinityTag, | 65 {'initializeDispatchProperty': backend.needToInitializeDispatchProperty, |
| 61 'initializeDispatchProperty': initializeDispatchProperty, | 66 'convertToFastObject': convertToFastObject, |
| 62 'getIsolateTag': getIsolateTagAccess, | 67 'getIsolateTag': getIsolateTagAccess, |
| 63 'isolateTag': isolateTagAccess, | 68 'isolateTag': isolateTagAccess, |
| 64 'dispatchPropertyName': dispatchPropertyNameAccess}); | 69 'dispatchPropertyName': dispatchPropertyNameAccess}); |
| 65 } | 70 } |
| 66 | 71 |
| 67 static String generateIsolateTagRoot() { | 72 static String generateIsolateTagRoot() { |
| 68 // TODO(sra): MD5 of contributing source code or URIs? | 73 // TODO(sra): MD5 of contributing source code or URIs? |
| 69 return 'ZxYxX'; | 74 return 'ZxYxX'; |
| 70 } | 75 } |
| 71 } | 76 } |
| OLD | NEW |