OLD | NEW |
| (Empty) |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 (function(global, utils) { | |
6 | |
7 "use strict"; | |
8 | |
9 %CheckIsBootstrapping(); | |
10 | |
11 // ------------------------------------------------------------------- | |
12 // Imports | |
13 | |
14 var GlobalFunction = global.Function; | |
15 var NewFunctionString; | |
16 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | |
17 | |
18 utils.Import(function(from) { | |
19 NewFunctionString = from.NewFunctionString; | |
20 }); | |
21 | |
22 // ---------------------------------------------------------------------------- | |
23 | |
24 // Generator functions and objects are specified by ES6, sections 15.19.3 and | |
25 // 15.19.4. | |
26 | |
27 function GeneratorObjectNext(value) { | |
28 if (!IS_GENERATOR(this)) { | |
29 throw MakeTypeError(kIncompatibleMethodReceiver, | |
30 '[Generator].prototype.next', this); | |
31 } | |
32 | |
33 var continuation = %GeneratorGetContinuation(this); | |
34 if (continuation > 0) { | |
35 // Generator is suspended. | |
36 if (DEBUG_IS_ACTIVE) %DebugPrepareStepInIfStepping(this); | |
37 try { | |
38 return %_GeneratorNext(this, value); | |
39 } catch (e) { | |
40 %GeneratorClose(this); | |
41 throw e; | |
42 } | |
43 } else if (continuation == 0) { | |
44 // Generator is already closed. | |
45 return { value: void 0, done: true }; | |
46 } else { | |
47 // Generator is running. | |
48 throw MakeTypeError(kGeneratorRunning); | |
49 } | |
50 } | |
51 | |
52 | |
53 function GeneratorObjectThrow(exn) { | |
54 if (!IS_GENERATOR(this)) { | |
55 throw MakeTypeError(kIncompatibleMethodReceiver, | |
56 '[Generator].prototype.throw', this); | |
57 } | |
58 | |
59 var continuation = %GeneratorGetContinuation(this); | |
60 if (continuation > 0) { | |
61 // Generator is suspended. | |
62 try { | |
63 return %_GeneratorThrow(this, exn); | |
64 } catch (e) { | |
65 %GeneratorClose(this); | |
66 throw e; | |
67 } | |
68 } else if (continuation == 0) { | |
69 // Generator is already closed. | |
70 throw exn; | |
71 } else { | |
72 // Generator is running. | |
73 throw MakeTypeError(kGeneratorRunning); | |
74 } | |
75 } | |
76 | |
77 | |
78 function GeneratorFunctionConstructor(arg1) { // length == 1 | |
79 var source = NewFunctionString(arguments, 'function*'); | |
80 var global_proxy = %GlobalProxy(GeneratorFunctionConstructor); | |
81 // Compile the string in the constructor and not a helper so that errors | |
82 // appear to come from here. | |
83 var f = %_CallFunction(global_proxy, %CompileString(source, true)); | |
84 %FunctionMarkNameShouldPrintAsAnonymous(f); | |
85 return f; | |
86 } | |
87 | |
88 // ---------------------------------------------------------------------------- | |
89 | |
90 // Both Runtime_GeneratorNext and Runtime_GeneratorThrow are supported by | |
91 // neither Crankshaft nor TurboFan, disable optimization of wrappers here. | |
92 %NeverOptimizeFunction(GeneratorObjectNext); | |
93 %NeverOptimizeFunction(GeneratorObjectThrow); | |
94 | |
95 // Set up non-enumerable functions on the generator prototype object. | |
96 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; | |
97 utils.InstallFunctions(GeneratorObjectPrototype, | |
98 DONT_ENUM, | |
99 ["next", GeneratorObjectNext, | |
100 "throw", GeneratorObjectThrow]); | |
101 | |
102 %AddNamedProperty(GeneratorObjectPrototype, "constructor", | |
103 GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY); | |
104 %AddNamedProperty(GeneratorObjectPrototype, | |
105 toStringTagSymbol, "Generator", DONT_ENUM | READ_ONLY); | |
106 %InternalSetPrototype(GeneratorFunctionPrototype, GlobalFunction.prototype); | |
107 %AddNamedProperty(GeneratorFunctionPrototype, | |
108 toStringTagSymbol, "GeneratorFunction", DONT_ENUM | READ_ONLY); | |
109 %AddNamedProperty(GeneratorFunctionPrototype, "constructor", | |
110 GeneratorFunction, DONT_ENUM | READ_ONLY); | |
111 %InternalSetPrototype(GeneratorFunction, GlobalFunction); | |
112 %SetCode(GeneratorFunction, GeneratorFunctionConstructor); | |
113 | |
114 }) | |
OLD | NEW |