| 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 | 8 |
| 9 static bool needsIsolateAffinityTagInitialization(JavaScriptBackend backend) { |
| 10 return backend.needToInitializeIsolateAffinityTag; |
| 11 } |
| 12 |
| 9 /// Generates the code for isolate affinity tags. | 13 /// Generates the code for isolate affinity tags. |
| 10 /// | 14 /// |
| 11 /// Independently Dart programs on the same page must not interfer and | 15 /// Independently Dart programs on the same page must not interfer and |
| 12 /// this code sets up the variables needed to guarantee that behavior. | 16 /// this code sets up the variables needed to guarantee that behavior. |
| 13 static jsAst.Statement generateIsolateAffinityTagInitialization( | 17 static jsAst.Statement generateIsolateAffinityTagInitialization( |
| 18 JavaScriptBackend backend, |
| 14 jsAst.Expression generateEmbeddedGlobalAccess(String global), | 19 jsAst.Expression generateEmbeddedGlobalAccess(String global), |
| 15 {bool initializeIsolateAffinityTag, | 20 jsAst.Expression convertToFastObject) { |
| 16 bool initializeDispatchProperty}) { | 21 assert(backend.needToInitializeIsolateAffinityTag); |
| 17 assert(initializeIsolateAffinityTag != null); | |
| 18 assert(initializeDispatchProperty != null); | |
| 19 assert(!initializeDispatchProperty || initializeIsolateAffinityTag); | |
| 20 | 22 |
| 21 jsAst.Expression getIsolateTagAccess = | 23 jsAst.Expression getIsolateTagAccess = |
| 22 generateEmbeddedGlobalAccess(embeddedNames.GET_ISOLATE_TAG); | 24 generateEmbeddedGlobalAccess(embeddedNames.GET_ISOLATE_TAG); |
| 23 jsAst.Expression isolateTagAccess = | 25 jsAst.Expression isolateTagAccess = |
| 24 generateEmbeddedGlobalAccess(embeddedNames.ISOLATE_TAG); | 26 generateEmbeddedGlobalAccess(embeddedNames.ISOLATE_TAG); |
| 25 jsAst.Expression dispatchPropertyNameAccess = | 27 jsAst.Expression dispatchPropertyNameAccess = |
| 26 generateEmbeddedGlobalAccess(embeddedNames.DISPATCH_PROPERTY_NAME); | 28 generateEmbeddedGlobalAccess(embeddedNames.DISPATCH_PROPERTY_NAME); |
| 27 | 29 |
| 28 return js.statement(''' | 30 return js.statement(''' |
| 29 if (#initializeIsolateAffinityTag) | |
| 30 !function() { | 31 !function() { |
| 31 // 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 |
| 32 // makes property access much faster. | 33 // makes property access much faster. |
| 33 function intern(s) { | 34 function intern(s) { |
| 34 var o = {}; | 35 var o = {}; |
| 35 o[s] = 1; | 36 o[s] = 1; |
| 36 return Object.keys(convertToFastObject(o))[0]; | 37 return Object.keys(#convertToFastObject(o))[0]; |
| 37 } | 38 } |
| 38 | 39 |
| 39 #getIsolateTag = function(name) { | 40 #getIsolateTag = function(name) { |
| 40 return intern("___dart_" + name + #isolateTag); | 41 return intern("___dart_" + name + #isolateTag); |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 // To ensure that different programs loaded into the same context (page) | 44 // To ensure that different programs loaded into the same context (page) |
| 44 // use distinct dispatch properies, we place an object on `Object` to | 45 // use distinct dispatch properies, we place an object on `Object` to |
| 45 // contain the names already in use. | 46 // contain the names already in use. |
| 46 var tableProperty = "___dart_isolate_tags_"; | 47 var tableProperty = "___dart_isolate_tags_"; |
| 47 var usedProperties = Object[tableProperty] || | 48 var usedProperties = Object[tableProperty] || |
| 48 (Object[tableProperty] = Object.create(null)); | 49 (Object[tableProperty] = Object.create(null)); |
| 49 | 50 |
| 50 var rootProperty = "_${generateIsolateTagRoot()}"; | 51 var rootProperty = "_${generateIsolateTagRoot()}"; |
| 51 for (var i = 0; ; i++) { | 52 for (var i = 0; ; i++) { |
| 52 var property = intern(rootProperty + "_" + i + "_"); | 53 var property = intern(rootProperty + "_" + i + "_"); |
| 53 if (!(property in usedProperties)) { | 54 if (!(property in usedProperties)) { |
| 54 usedProperties[property] = 1; | 55 usedProperties[property] = 1; |
| 55 #isolateTag = property; | 56 #isolateTag = property; |
| 56 break; | 57 break; |
| 57 } | 58 } |
| 58 } | 59 } |
| 59 if (#initializeDispatchProperty) { | 60 if (#initializeDispatchProperty) { |
| 60 #dispatchPropertyName = #getIsolateTag("dispatch_record"); | 61 #dispatchPropertyName = #getIsolateTag("dispatch_record"); |
| 61 } | 62 } |
| 62 }(); | 63 }(); |
| 63 ''', | 64 ''', |
| 64 {'initializeIsolateAffinityTag': initializeIsolateAffinityTag, | 65 {'initializeDispatchProperty': backend.needToInitializeDispatchProperty, |
| 65 'initializeDispatchProperty': initializeDispatchProperty, | 66 'convertToFastObject': convertToFastObject, |
| 66 'getIsolateTag': getIsolateTagAccess, | 67 'getIsolateTag': getIsolateTagAccess, |
| 67 'isolateTag': isolateTagAccess, | 68 'isolateTag': isolateTagAccess, |
| 68 'dispatchPropertyName': dispatchPropertyNameAccess}); | 69 'dispatchPropertyName': dispatchPropertyNameAccess}); |
| 69 } | 70 } |
| 70 | 71 |
| 71 static String generateIsolateTagRoot() { | 72 static String generateIsolateTagRoot() { |
| 72 // TODO(sra): MD5 of contributing source code or URIs? | 73 // TODO(sra): MD5 of contributing source code or URIs? |
| 73 return 'ZxYxX'; | 74 return 'ZxYxX'; |
| 74 } | 75 } |
| 75 } | 76 } |
| OLD | NEW |