OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #include <iostream> | |
5 #include "src/builtins/builtins-utils.h" | |
6 #include "src/builtins/builtins.h" | |
7 | |
8 namespace v8 { | |
9 namespace internal { | |
10 | |
11 BUILTIN(PromiseResolveClosure) { | |
12 HandleScope scope(isolate); | |
13 | |
14 Handle<Object> value = args.atOrUndefined(isolate, 1); | |
15 Context* context = isolate->context(); | |
adamk
2016/11/08 18:09:55
This should be stored into a Handle<Context> immed
gsathya
2016/11/08 20:03:18
Done.
| |
16 Handle<Cell> already_visited(Cell::cast(context->get(4)), isolate); | |
adamk
2016/11/08 18:09:55
These magic numbers have to be defined as constant
gsathya
2016/11/08 20:03:18
Done.
| |
17 | |
18 if (Smi::cast(already_visited->value())->value() != 0) { | |
19 return isolate->heap()->undefined_value(); | |
20 } | |
21 | |
22 already_visited->set_value(Smi::FromInt(1)); | |
23 Handle<JSObject> promise(JSObject::cast(context->get(5)), isolate); | |
24 | |
25 MaybeHandle<Object> maybe_result; | |
26 Handle<Object> argv[] = {promise, value}; | |
27 maybe_result = Execution::Call(isolate, isolate->promise_resolve(), | |
28 isolate->factory()->undefined_value(), | |
29 arraysize(argv), argv); | |
30 return isolate->heap()->undefined_value(); | |
31 } | |
32 | |
33 BUILTIN(PromiseRejectClosure) { | |
34 HandleScope scope(isolate); | |
35 | |
36 Handle<Object> value = args.atOrUndefined(isolate, 1); | |
37 Context* context = isolate->context(); | |
38 Handle<Cell> already_visited(Cell::cast(context->get(4)), isolate); | |
39 | |
40 if (Smi::cast(already_visited->value())->value() != 0) { | |
41 return isolate->heap()->undefined_value(); | |
42 } | |
43 | |
44 already_visited->set_value(Smi::FromInt(1)); | |
45 Handle<JSObject> promise(JSObject::cast(context->get(5)), isolate); | |
46 Handle<Object> debug_event(context->get(6), isolate); | |
47 MaybeHandle<Object> maybe_result; | |
48 Handle<Object> argv[] = {promise, value, debug_event}; | |
49 maybe_result = Execution::Call(isolate, isolate->promise_internal_reject(), | |
50 isolate->factory()->undefined_value(), | |
51 arraysize(argv), argv); | |
52 return isolate->heap()->undefined_value(); | |
53 } | |
54 | |
55 namespace { | |
56 Handle<JSFunction> CreateClosure(Isolate* isolate, | |
57 Handle<SharedFunctionInfo> info, | |
58 Handle<Cell> already_resolved, | |
59 Handle<JSObject> promise, | |
60 Handle<Object> debug_event) { | |
61 Handle<JSFunction> fun = | |
62 isolate->factory()->NewFunctionFromSharedFunctionInfo( | |
63 isolate->sloppy_function_without_prototype_map(), info, | |
64 isolate->native_context(), TENURED); | |
65 | |
66 Handle<Context> context = | |
67 isolate->factory()->NewPromiseResolvingFunctionContext( | |
adamk
2016/11/08 18:09:55
Couldn't you share this context between the two fu
gsathya
2016/11/08 20:03:18
Done. But now this context is completely busted (l
| |
68 fun, already_resolved, promise, debug_event); | |
69 fun->set_context(*context); | |
70 return fun; | |
71 } | |
72 } // namespace | |
73 | |
74 BUILTIN(CreateResolvingFunctions) { | |
75 HandleScope scope(isolate); | |
76 | |
77 Handle<Object> promise_obj = args.atOrUndefined(isolate, 1); | |
78 Handle<Object> debug_event = args.atOrUndefined(isolate, 2); | |
adamk
2016/11/08 18:09:55
atOrUndefined seems wrong here, as this is only ca
gsathya
2016/11/08 20:03:19
Nice! TIL args.at
| |
79 | |
80 Handle<JSObject> promise = Handle<JSObject>::cast(promise_obj); | |
81 Handle<Cell> already_resolved = | |
82 isolate->factory()->NewCell(handle(Smi::kZero, isolate)); | |
83 Handle<SharedFunctionInfo> resolve_shared_fun( | |
84 isolate->native_context()->promise_resolve_shared_fun(), isolate); | |
85 Handle<SharedFunctionInfo> reject_shared_fun( | |
86 isolate->native_context()->promise_reject_shared_fun(), isolate); | |
87 | |
88 Handle<JSFunction> resolve = CreateClosure( | |
89 isolate, resolve_shared_fun, already_resolved, promise, debug_event); | |
90 Handle<JSFunction> reject = CreateClosure( | |
91 isolate, reject_shared_fun, already_resolved, promise, debug_event); | |
92 | |
93 Handle<JSObject> result = | |
94 isolate->factory()->NewJSObject(isolate->object_function(), NOT_TENURED); | |
adamk
2016/11/08 18:09:55
What might be even faster would be to change this
gsathya
2016/11/08 20:03:18
Done.
| |
95 JSObject::AddProperty(result, isolate->factory()->resolve_string(), resolve, | |
96 NONE); | |
97 | |
98 JSObject::AddProperty(result, isolate->factory()->reject_string(), reject, | |
99 NONE); | |
100 | |
101 return *result; | |
102 } | |
103 | |
104 } // namespace internal | |
105 } // namespace v8 | |
OLD | NEW |