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