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