Commit 2f8f5881 authored by Linshizhi's avatar Linshizhi

Add nativeBox unittest.

parent a90da11d
......@@ -146,10 +146,10 @@ blink_core_sources_bindings =
"core/v8/v8_idle_task_runner.h",
"core/v8/v8_initializer.cc",
"core/v8/v8_initializer.h",
#"core/v8/v8_initializer_custom.cc",
#"core/v8/v8_initializer_custom.h",
#"core/v8/nativeBox.h",
#"core/v8/nativeBox.cc",
"core/v8/v8_initializer_custom.cc",
"core/v8/v8_initializer_custom.h",
"core/v8/nativeBox.h",
"core/v8/nativeBox.cc",
"extensions/loadablemodule/src/Loadable.cc",
"extensions/loadablemodule/src/Loadable.h",
"core/v8/v8_intersection_observer_delegate.cc",
......@@ -228,6 +228,7 @@ bindings_unittest_files = get_path_info(
"core/v8/script_wrappable_v8_gc_integration_test.cc",
"core/v8/to_v8_test.cc",
"core/v8/to_v8_traits_test.cc",
"core/v8/nativeBox_unittest.cc",
"core/v8/trace_wrapper_v8_reference_test.cc",
"core/v8/v8_binding_for_testing.cc",
"core/v8/v8_binding_for_testing.h",
......
......@@ -9,6 +9,7 @@
#include "v8/include/v8-typed-array.h"
#include "v8/include/v8-value.h"
#include "v8/include/v8-object.h"
#include "v8/include/v8-maybe.h"
using std::vector;
......@@ -19,72 +20,63 @@ using v8::Uint32;
using v8::Object;
using v8::Local;
namespace L = Loadable;
#if 0
// Anonymous which contains all transform functions
namespace {
typedef void (*V8_Builtin)(const v8::FunctionCallbackInfo<v8::Value>& args);
V8_Builtin wrapWithTransformLayer(Loadable::Callback callback,
vector<Loadable::ArgType> &argInfo,
Loadable::ArgType retInfo) {
auto f = [](const v8::FunctionCallbackInfo<v8::Value> &args) -> void {
};
return f;
}
NativeBox::NativeBox(L::LoadableModule *module): native(module) {}
}
NativeBox::NativeBox(Loadable::LoadableModule *module): native(module) {}
Local<Object> NativeBox::ToObject(v8::Isolate *isolate) {
// Find out an anatomy of LoadableModule
// and spawn correspond JSObject which is equivalent to
// LoadableModule in aspect of interfaces.
Local<Object> obj = Local<Object>();
Local<Object> obj = v8::Object::New(isolate);
if (!native) {
// No LoadableModule found
// return null Handle<JSObject> .
return obj;
return Local<Object>();
}
// Analyze LoadableModule
vector<L::IFaceSpec> IFSpecs = native->specs();
for (vector<L::IFaceSpec>::iterator iter = IFSpecs.begin();
iter < IFSpecs.end(); ++iter) {
// Get return type
L::ArgType retType = iter->getRet();
// Get type of arguments
std::vector<L::ArgType> args = iter->getArgs();
void *callback = iter->getCallback();
// Spawn correspond method and attach the method to
// spawned JSObj.
auto transform_layer = [&](const v8::FunctionCallbackInfo<v8::Value>& args_) -> void {
if (args.size() != args_.Length()) return;
int i = 0;
// Transform argument into native.
for (std::vector<L::ArgType>::iterator iter = args.begin();
iter < args.end(); ++iter) {
switch (*iter) {
case L::ARG_TYPE_NUM:
transformToNative<uint32_t>(args[i]);
break;
case L::ARG_TYPE_STR:
transformToNative<std::string>(args[i]);
break;
case L::ARG_TYPE_ARRAY:
transformToNative<uint8_t*>(args[i]);
break;
}
++i;
}
};
vector<Loadable::IFaceSpec> IFSpecs = native->specs();
Local<v8::Context> context = isolate->GetCurrentContext();
for (auto &iface: IFSpecs) {
// Wrap native codes with an extra Transform layer.
//
// Transform layer provide functionality of transforming
// between v8 types and native types.
vector<Loadable::ArgType> args = iface.getArgs();
V8_Builtin nativeWitTrans = wrapWithTransformLayer(
iface.getCallback(), args, iface.getRet());
// Attach V8_Builtin to Object which defined at the first line of
// this procedure.
Local<Value> functionName = v8::String::NewFromUtf8(
isolate, iface.getName().c_str()).ToLocalChecked();
Local<Value> jsFunction = v8::FunctionTemplate::New(isolate, nativeWitTrans)
->GetFunction(context).ToLocalChecked();
v8::Maybe<bool> ret = obj->Set(context, functionName, jsFunction);
if (ret.IsJust()) {
return Local<Object>();
}
}
return obj;
}
#endif
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/extensions/loadablemodule/src/Loadable.h"
#include "third_party/blink/renderer/bindings/core/v8/nativeBox.h"
namespace blink{
TEST(NativeBoxTest, Transform) {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment