Skip to content

Commit

Permalink
Converting native object from javascript dictionary type (#219)
Browse files Browse the repository at this point in the history
When javascript dictionary type passed as parameter,
it should be pass as native object at cpp side.
so it is need to convert to native object.
present, this CL has test which take double type.
so we should add other tests for other primitive types.

ISSUE=#169
  • Loading branch information
hwanseung authored and romandev committed Dec 15, 2017
1 parent 61092e5 commit 9d49a40
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
14 changes: 13 additions & 1 deletion template/interface_cpp.njk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ if (!EnumValidator::isValildEnum({{argument.name}}, enum_value_set)) {
}
{% elif argument.dictionary %}
{{argument.type | pascalcase}} {{argument.name}};
// FIXME(hwanseung): Convert to native dictionary object from javascript object.
if (!info[{{loop.index0}}].IsObject()) {
Napi::TypeError::New(info.Env(), "it is not object type.")
.ThrowAsJavaScriptException();
}
Napi::Object {{argument.name}}Obj = info[{{loop.index0}}].ToObject();
{% for member in argument.dictionary.members %}
if ({{argument.name}}Obj.Has("{{member.name}}")) {
Napi::Value value = {{argument.name}}Obj.Get("{{member.name}}");
auto {{member.name}} = NativeTypeTraits<IDL{{member.type | pascalcase-}}
>::NativeValue(info.Env(), value);
{{argument.name}}.set{{member.name | pascalcase}}({{member.name}});
}
{% endfor %}
{% else %}
auto {{argument.name}} = NativeTypeTraits<IDL{{argument.type | pascalcase-}}
>::NativeValue(info.Env(), info[{{loop.index0}}]);
Expand Down
6 changes: 3 additions & 3 deletions test/interface_dictionary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test(
let test_interface = new bacardi.TestInterface();

var testDict = {a: 10};
test_interface.voidMethodTestDictionaryArg(testDict);
expect(test_interface.doubleMethodTestDictionaryArg(testDict)).toBe(10);
expect(bacardi.TestInterface.getLastCallInfo())
.toBe('VoidMethodTestDictionaryArg()');
});
.toBe('DoubleMethodTestDictionaryArg()');
});
5 changes: 3 additions & 2 deletions test/test_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void TestInterface::SetStaticDoubleNumber(double number) {
TestInterface::static_double_number_ = number;
}

void TestInterface::VoidMethodTestDictionaryArg(TestDict testDict) {
last_call_info_ = "VoidMethodTestDictionaryArg()";
double TestInterface::DoubleMethodTestDictionaryArg(TestDict testDict) {
last_call_info_ = "DoubleMethodTestDictionaryArg()";
return testDict.a();
}
2 changes: 1 addition & 1 deletion test/test_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TestInterface {
static void SetStaticDoubleNumber(double number);

// Dictionary
void VoidMethodTestDictionaryArg(TestDict testDict);
double DoubleMethodTestDictionaryArg(TestDict testDict);

private:
// FIXME(zino): Currently, we should set this variable in each methods. It's
Expand Down
2 changes: 1 addition & 1 deletion test/test_interface.idl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface TestInterface {
static double StaticTest(double number);

// Dictionary
void voidMethodTestDictionaryArg(TestDict dicValue);
double doubleMethodTestDictionaryArg(TestDict dicValue);
};

enum TestEnum {
Expand Down

0 comments on commit 9d49a40

Please sign in to comment.