OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 js_backend; | 5 part of js_backend; |
6 | 6 |
7 /// Enables debugging of fast/slow objects using V8-specific primitives. | 7 /// Enables debugging of fast/slow objects using V8-specific primitives. |
8 const DEBUG_FAST_OBJECTS = false; | 8 const DEBUG_FAST_OBJECTS = false; |
9 | 9 |
10 /** | 10 /** |
(...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 'existingIsolateProperties[superclass]')), | 852 'existingIsolateProperties[superclass]')), |
853 | 853 |
854 js('prototype = inheritFrom(constructor, superConstructor)'), | 854 js('prototype = inheritFrom(constructor, superConstructor)'), |
855 ]); | 855 ]); |
856 | 856 |
857 return new jsAst.FunctionDeclaration( | 857 return new jsAst.FunctionDeclaration( |
858 new jsAst.VariableDeclaration('finishClass'), | 858 new jsAst.VariableDeclaration('finishClass'), |
859 fun); | 859 fun); |
860 } | 860 } |
861 | 861 |
862 jsAst.Fun get finishIsolateConstructorFunction_NO_CSP { | |
863 String isolate = namer.isolateName; | |
864 // We replace the old Isolate function with a new one that initializes | |
865 // all its field with the initial (and often final) value of all globals. | |
866 // This has two advantages: | |
867 // 1. the properties are in the object itself (thus avoiding to go through | |
868 // the prototype when looking up globals. | |
869 // 2. a new isolate goes through a (usually well optimized) constructor | |
870 // function of the form: "function() { this.x = ...; this.y = ...; }". | |
871 // | |
872 // Example: If [isolateProperties] is an object containing: x = 3 and | |
873 // A = function A() { /* constructor of class A. */ }, then we generate: | |
874 // str = "{ | |
875 // var isolateProperties = Isolate.$isolateProperties; | |
876 // this.x = isolateProperties.x; | |
877 // this.A = isolateProperties.A; | |
878 // }"; | |
879 // which is then dynamically evaluated: | |
880 // var newIsolate = new Function(str); | |
881 // | |
882 // We also copy over old values like the prototype, and the | |
883 // isolateProperties themselves. | |
884 | |
885 List copyFinishClasses = []; | |
886 if (needsDefineClass) { | |
887 copyFinishClasses.add( | |
888 js('newIsolate.$finishClassesProperty = ' | |
889 ' oldIsolate.$finishClassesProperty')); | |
890 } | |
891 | |
892 // function(oldIsolate) { | |
893 return js.fun('oldIsolate', [ | |
894 js('var isolateProperties = oldIsolate.${namer.isolatePropertiesName}'), | |
895 | |
896 js('var isolatePrototype = oldIsolate.prototype'), | |
897 js('var str = "{\\n"'), | |
898 js('str += "var properties = ' | |
899 'arguments.callee.${namer.isolatePropertiesName};\\n"'), | |
900 js('var hasOwnProperty = Object.prototype.hasOwnProperty'), | |
901 | |
902 // for (var staticName in isolateProperties) { | |
903 js.forIn('staticName', 'isolateProperties', [ | |
904 js.if_('hasOwnProperty.call(isolateProperties, staticName)', [ | |
905 js('str += ("this." + staticName + "= properties." + staticName + ' | |
906 '";\\n")') | |
907 ]) | |
908 ]), | |
909 | |
910 js('str += "}\\n"'), | |
911 | |
912 js('var newIsolate = new Function(str)'), | |
913 js('newIsolate.prototype = isolatePrototype'), | |
914 js('isolatePrototype.constructor = newIsolate'), | |
915 js('newIsolate.${namer.isolatePropertiesName} = isolateProperties'), | |
916 // TODO(ahe): Only copy makeConstantList when it is used. | |
917 js('newIsolate.makeConstantList = oldIsolate.makeConstantList'), | |
918 ]..addAll(copyFinishClasses) | |
919 ..addAll([ | |
920 | |
921 // return newIsolate; | |
922 js.return_('newIsolate') | |
923 ])); | |
924 } | |
925 | |
926 jsAst.Fun get finishIsolateConstructorFunction { | 862 jsAst.Fun get finishIsolateConstructorFunction { |
927 // We replace the old Isolate function with a new one that initializes | 863 // We replace the old Isolate function with a new one that initializes |
928 // all its fields with the initial (and often final) value of all globals. | 864 // all its fields with the initial (and often final) value of all globals. |
929 // | 865 // |
930 // We also copy over old values like the prototype, and the | 866 // We also copy over old values like the prototype, and the |
931 // isolateProperties themselves. | 867 // isolateProperties themselves. |
932 return js.fun('oldIsolate', [ | 868 return js.fun('oldIsolate', [ |
933 js('var isolateProperties = oldIsolate.${namer.isolatePropertiesName}'), | 869 js('var isolateProperties = oldIsolate.${namer.isolatePropertiesName}'), |
934 new jsAst.FunctionDeclaration( | 870 new jsAst.FunctionDeclaration( |
935 new jsAst.VariableDeclaration('Isolate'), | 871 new jsAst.VariableDeclaration('Isolate'), |
(...skipping 2898 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3834 buffer.write(metadata); | 3770 buffer.write(metadata); |
3835 } | 3771 } |
3836 } else { | 3772 } else { |
3837 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; | 3773 throw 'Unexpected value in metadata: ${Error.safeToString(metadata)}'; |
3838 } | 3774 } |
3839 buffer.write(',$n'); | 3775 buffer.write(',$n'); |
3840 } | 3776 } |
3841 buffer.write('];$n'); | 3777 buffer.write('];$n'); |
3842 } | 3778 } |
3843 | 3779 |
3844 void emitConvertToFastObjectFunction_NO_CSP() { | |
3845 mainBuffer.add(r''' | |
3846 function convertToFastObject(properties) { | |
3847 function makeConstructor() { | |
3848 var str = "{\n"; | |
3849 var hasOwnProperty = Object.prototype.hasOwnProperty; | |
3850 for (var property in properties) { | |
3851 if (hasOwnProperty.call(properties, property)) { | |
3852 str += "this." + property + "= properties." + property + ";\n"; | |
3853 } | |
3854 } | |
3855 str += "}\n"; | |
3856 return new Function("properties", str); | |
3857 }; | |
3858 var constructor = makeConstructor(); | |
3859 return makeConstructor.prototype = new constructor(properties); | |
3860 } | |
3861 '''); | |
3862 } | |
3863 | |
3864 void emitConvertToFastObjectFunction() { | 3780 void emitConvertToFastObjectFunction() { |
3865 // Create an instance that uses 'properties' as prototype. This should make | 3781 // Create an instance that uses 'properties' as prototype. This should make |
3866 // 'properties' a fast object. | 3782 // 'properties' a fast object. |
3867 mainBuffer.add(r'''function convertToFastObject(properties) { | 3783 mainBuffer.add(r'''function convertToFastObject(properties) { |
3868 function MyClass() {}; | 3784 function MyClass() {}; |
3869 MyClass.prototype = properties; | 3785 MyClass.prototype = properties; |
3870 new MyClass(); | 3786 new MyClass(); |
3871 '''); | 3787 '''); |
3872 if (DEBUG_FAST_OBJECTS) { | 3788 if (DEBUG_FAST_OBJECTS) { |
3873 ClassElement primitives = | 3789 ClassElement primitives = |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4451 | 4367 |
4452 const String HOOKS_API_USAGE = """ | 4368 const String HOOKS_API_USAGE = """ |
4453 // The code supports the following hooks: | 4369 // The code supports the following hooks: |
4454 // dartPrint(message) - if this function is defined it is called | 4370 // dartPrint(message) - if this function is defined it is called |
4455 // instead of the Dart [print] method. | 4371 // instead of the Dart [print] method. |
4456 // dartMainRunner(main) - if this function is defined, the Dart [main] | 4372 // dartMainRunner(main) - if this function is defined, the Dart [main] |
4457 // method will not be invoked directly. | 4373 // method will not be invoked directly. |
4458 // Instead, a closure that will invoke [main] is | 4374 // Instead, a closure that will invoke [main] is |
4459 // passed to [dartMainRunner]. | 4375 // passed to [dartMainRunner]. |
4460 """; | 4376 """; |
OLD | NEW |