Chromium Code Reviews| Index: src/ffi/ffi-compiler.cc |
| diff --git a/src/ffi/ffi-compiler.cc b/src/ffi/ffi-compiler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4cc4d3bd69d871ee122de40b23492aa582b09d63 |
| --- /dev/null |
| +++ b/src/ffi/ffi-compiler.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2017 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "src/ffi/ffi-compiler.h" |
| +#include "src/api.h" |
| +#include "src/code-factory.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| + |
| +void InstallFFIMap(Isolate* isolate) { |
| + Handle<Context> context(isolate->context()); |
| + DCHECK(!context->get(Context::NATIVE_FUNCTION_MAP_INDEX)->IsMap()); |
| + Handle<Map> prev_map = Handle<Map>(context->sloppy_function_map(), isolate); |
| + |
| + InstanceType instance_type = prev_map->instance_type(); |
| + int internal_fields = JSObject::GetInternalFieldCount(*prev_map); |
| + CHECK_EQ(0, internal_fields); |
| + int pre_allocated = |
| + prev_map->GetInObjectProperties() - prev_map->unused_property_fields(); |
| + int instance_size; |
| + int in_object_properties; |
| + JSFunction::CalculateInstanceSizeHelper( |
| + instance_type, internal_fields, 0, &instance_size, &in_object_properties); |
| + int unused_property_fields = in_object_properties - pre_allocated; |
| + Handle<Map> map = Map::CopyInitialMap( |
| + prev_map, instance_size, in_object_properties, unused_property_fields); |
| + context->set_native_function_map(*map); |
| +} |
| + |
| +namespace ffi { |
| + |
| +Node* FFIAssembler::ToJS(Node* node, Node* context, MachineType type) { |
| + UNREACHABLE(); |
| + // TODO(mattloring): Needs to be implemented. |
| + return nullptr; |
| +} |
| + |
| +Node* FFIAssembler::FromJS(Node* node, Node* context, MachineType type) { |
| + UNREACHABLE(); |
| + // TODO(mattloring): Needs to be implemented. |
| + return nullptr; |
| +} |
| + |
| +Handle<Code> FFIAssembler::GenerateJSToNativeWrapper(NativeFunction* func) { |
| + int params = static_cast<int>(func->sig->parameter_count()); |
| + int returns = static_cast<int>(func->sig->return_count()); |
| + ApiFunction api_func(func->start); |
| + ExternalReference ref(&api_func, ExternalReference::DIRECT_API_CALL, |
| + isolate()); |
| + |
| + Node* context_param = GetJSContextParameter(); |
| + |
| + Node** inputs = zone()->NewArray<Node*>(params + 1); |
| + int input_count = 0; |
| + inputs[input_count++] = ExternalConstant(ref); |
| + for (int i = 0; i < params; i++) { |
| + inputs[input_count++] = |
| + FromJS(Parameter(i), context_param, func->sig->GetParam(i)); |
| + } |
| + |
| + Node* call = CallCFunctionN(func->sig, input_count, inputs); |
| + Node* return_val = UndefinedConstant(); |
| + if (returns == 1) { |
| + return_val = ToJS(call, context_param, func->sig->GetReturn()); |
| + } |
| + Return(return_val); |
| + |
| + 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.
|
| +} |
| + |
| +Handle<JSFunction> CompileJSToNativeWrapper(Isolate* isolate, |
| + Handle<String> name, |
| + NativeFunction func) { |
| + int params = static_cast<int>(func.sig->parameter_count()); |
| + Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo( |
| + name, MaybeHandle<Code>(), false); |
| + shared->set_length(params); |
| + shared->set_internal_formal_parameter_count(params); |
| + Handle<JSFunction> function = isolate->factory()->NewFunction( |
| + isolate->native_function_map(), name, MaybeHandle<Code>()); |
| + function->set_shared(*shared); |
| + |
| + Zone zone(isolate->allocator(), ZONE_NAME); |
| + CodeAssemblerState state(isolate, &zone, params, |
| + Code::ComputeFlags(Code::FUNCTION), "js-to-native"); |
| + FFIAssembler assembler(&state); |
| + Handle<Code> code = assembler.GenerateJSToNativeWrapper(&func); |
| + function->set_code(*code); |
| + return function; |
| +} |
| + |
| +} // namespace ffi |
| +} // namespace internal |
| +} // namespace v8 |