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