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 | |
5 #include "src/compiler/ffi-compiler.h" | |
6 | |
7 #include "src/isolate-inl.h" | |
8 | |
9 #include "src/api.h" | |
10 #include "src/code-factory.h" | |
11 #include "src/code-stub-assembler.h" | |
12 #include "src/compilation-info.h" | |
13 #include "src/compiler/linkage.h" | |
14 | |
15 namespace v8 { | |
16 namespace internal { | |
17 namespace compiler { | |
18 | |
19 // TODO(ofrobots): find a better home for this. | |
20 static void Setup(Isolate* isolate) { | |
21 Handle<Context> context(isolate->context()); | |
22 if (!context->get(Context::NATIVE_FUNCTION_MAP_INDEX)->IsMap()) { | |
23 // TODO(ofrobots): move this to boostrapper.cc?? | |
24 Handle<Map> prev_map = Handle<Map>(context->sloppy_function_map(), isolate); | |
25 | |
26 InstanceType instance_type = prev_map->instance_type(); | |
27 int internal_fields = JSObject::GetInternalFieldCount(*prev_map); | |
28 CHECK_EQ(0, internal_fields); | |
29 int pre_allocated = | |
30 prev_map->GetInObjectProperties() - prev_map->unused_property_fields(); | |
31 int instance_size; | |
32 int in_object_properties; | |
33 JSFunction::CalculateInstanceSizeHelper(instance_type, internal_fields, 0, | |
34 &instance_size, | |
35 &in_object_properties); | |
36 int unused_property_fields = in_object_properties - pre_allocated; | |
37 Handle<Map> map = Map::CopyInitialMap( | |
38 prev_map, instance_size, in_object_properties, unused_property_fields); | |
39 context->set_native_function_map(*map); | |
40 } | |
41 } | |
42 | |
43 Node* ToJS(CodeStubAssembler* assembler, Node* node, Node* context, | |
44 MachineType type) { | |
45 UNREACHABLE(); | |
ofrobots
2017/01/12 19:29:47
Add a // TODO: needs to be implemented
mattloring
2017/01/12 19:40:11
Done.
| |
46 return nullptr; | |
47 } | |
48 | |
49 Node* FromJS(CodeStubAssembler* assembler, Node* node, Node* context, | |
50 MachineType type) { | |
51 UNREACHABLE(); | |
ofrobots
2017/01/12 19:29:47
Likewise.
mattloring
2017/01/12 19:40:11
Done.
| |
52 return nullptr; | |
53 } | |
54 | |
55 Handle<JSFunction> CompileJSToNativeWrapper(Isolate* isolate, | |
56 Handle<String> name, | |
57 ffi::NativeFunction func) { | |
58 Setup(isolate); | |
59 | |
60 int params = static_cast<int>(func.sig->parameter_count()); | |
61 int returns = static_cast<int>(func.sig->return_count()); | |
62 Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo( | |
63 name, MaybeHandle<Code>(), false); | |
64 shared->set_length(params); | |
65 shared->set_internal_formal_parameter_count(params); | |
66 Handle<JSFunction> function = isolate->factory()->NewFunction( | |
67 isolate->native_function_map(), name, MaybeHandle<Code>()); | |
68 function->set_shared(*shared); | |
69 | |
70 Zone zone(isolate->allocator(), ZONE_NAME); | |
71 int params_with_recv = params + 1; | |
72 CodeAssemblerState state(isolate, &zone, params_with_recv, | |
73 Code::ComputeFlags(Code::FUNCTION), "js-to-native"); | |
74 CodeStubAssembler assembler(&state); | |
75 | |
76 CallDescriptor* cdesc = Linkage::GetSimplifiedCDescriptor(&zone, func.sig); | |
77 ApiFunction api_func(func.start); | |
78 ExternalReference ref(&api_func, ExternalReference::DIRECT_API_CALL, isolate); | |
79 | |
80 Node* context_param = assembler.Parameter( | |
81 Linkage::GetJSCallContextParamIndex(params_with_recv)); | |
82 | |
83 Node** inputs = zone.NewArray<Node*>(params + 1); | |
84 int input_count = 0; | |
85 inputs[input_count++] = assembler.ExternalConstant(ref); | |
86 for (int i = 0; i < params; i++) { | |
87 inputs[input_count++] = FromJS(&assembler, assembler.Parameter(i), | |
88 context_param, func.sig->GetParam(i)); | |
89 } | |
90 | |
91 Node* call = assembler.CallCFunctionN(cdesc, input_count, inputs); | |
92 Node* return_val = assembler.UndefinedConstant(); | |
93 if (returns == 1) { | |
94 return_val = ToJS(&assembler, call, context_param, func.sig->GetReturn()); | |
95 } | |
96 assembler.Return(return_val); | |
97 | |
98 Handle<Code> code = CodeAssembler::GenerateCode(&state); | |
99 function->set_code(*code); | |
100 return function; | |
101 } | |
102 | |
103 } // namespace compiler | |
104 } // namespace internal | |
105 } // namespace v8 | |
OLD | NEW |