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(mattloring): 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(mattloring): 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(); | |
46 // TODO(mattloring): Needs to be implemented. | |
47 return nullptr; | |
48 } | |
49 | |
50 Node* FromJS(CodeStubAssembler* assembler, Node* node, Node* context, | |
51 MachineType type) { | |
52 UNREACHABLE(); | |
53 // TODO(mattloring): Needs to be implemented. | |
54 return nullptr; | |
55 } | |
56 | |
57 Handle<JSFunction> CompileJSToNativeWrapper(Isolate* isolate, | |
58 Handle<String> name, | |
59 ffi::NativeFunction func) { | |
60 Setup(isolate); | |
61 | |
62 int params = static_cast<int>(func.sig->parameter_count()); | |
63 int returns = static_cast<int>(func.sig->return_count()); | |
64 Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo( | |
65 name, MaybeHandle<Code>(), false); | |
66 shared->set_length(params); | |
67 shared->set_internal_formal_parameter_count(params); | |
68 Handle<JSFunction> function = isolate->factory()->NewFunction( | |
69 isolate->native_function_map(), name, MaybeHandle<Code>()); | |
70 function->set_shared(*shared); | |
71 | |
72 Zone zone(isolate->allocator(), ZONE_NAME); | |
73 int params_with_recv = params + 1; | |
74 CodeAssemblerState state(isolate, &zone, params_with_recv, | |
75 Code::ComputeFlags(Code::FUNCTION), "js-to-native"); | |
76 CodeStubAssembler assembler(&state); | |
77 | |
78 CallDescriptor* cdesc = Linkage::GetSimplifiedCDescriptor(&zone, func.sig); | |
79 ApiFunction api_func(func.start); | |
80 ExternalReference ref(&api_func, ExternalReference::DIRECT_API_CALL, isolate); | |
81 | |
82 Node* context_param = assembler.Parameter( | |
83 Linkage::GetJSCallContextParamIndex(params_with_recv)); | |
Michael Starzinger
2017/01/16 12:32:00
Once the FFI-compiler moves to outside of the "com
| |
84 | |
85 Node** inputs = zone.NewArray<Node*>(params + 1); | |
86 int input_count = 0; | |
87 inputs[input_count++] = assembler.ExternalConstant(ref); | |
88 for (int i = 0; i < params; i++) { | |
89 inputs[input_count++] = FromJS(&assembler, assembler.Parameter(i), | |
90 context_param, func.sig->GetParam(i)); | |
91 } | |
92 | |
93 Node* call = assembler.CallCFunctionN(cdesc, input_count, inputs); | |
94 Node* return_val = assembler.UndefinedConstant(); | |
95 if (returns == 1) { | |
96 return_val = ToJS(&assembler, call, context_param, func.sig->GetReturn()); | |
97 } | |
98 assembler.Return(return_val); | |
99 | |
100 Handle<Code> code = CodeAssembler::GenerateCode(&state); | |
101 function->set_code(*code); | |
102 return function; | |
103 } | |
104 | |
105 } // namespace compiler | |
106 } // namespace internal | |
107 } // namespace v8 | |
OLD | NEW |