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