Skip to content

Commit

Permalink
Fixes OutputList API
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni committed Mar 2, 2021
1 parent 8bf727e commit 3b23f8d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
2 changes: 2 additions & 0 deletions include/kth/js-native/chain/output_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace kth::js_native {

void chain_output_list_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_output_list_push_back(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_output_list_destruct(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_output_list_count(v8::FunctionCallbackInfo<v8::Value> const& args);
void chain_output_list_nth(v8::FunctionCallbackInfo<v8::Value> const& args);

Expand Down
70 changes: 46 additions & 24 deletions src/chain/output_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Global;

using v8::Object;
using v8::String;
using v8::Boolean;
Expand All @@ -30,78 +29,101 @@ using v8::Function;
using v8::Uint8Array;
using v8::ArrayBuffer;

void chain_output_list_construct_default(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 0) {
throw_exception(isolate, "Wrong number of arguments. chain_output_list_construct_default function requires 0 arguments.");
return;
}

kth_output_list_t res = kth_chain_output_list_construct_default();
args.GetReturnValue().Set(External::New(isolate, res));
}

void chain_output_list_push_back(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 2) {
throw_exception(isolate, "Wrong number of arguments");
throw_exception(isolate, "Wrong number of arguments. chain_output_list_push_back function requires 2 arguments.");
return;
}

if ( ! args[0]->IsExternal()) {
throw_exception(isolate, "Wrong arguments");
throw_exception(isolate, "Wrong argument type for argument list (#1). Required to be IsExternal.");
return;
}

if ( ! args[1]->IsExternal()) {
throw_exception(isolate, "Wrong arguments");
throw_exception(isolate, "Wrong argument type for argument elem (#2). Required to be IsExternal.");
return;
}

kth_output_list_t list = (kth_output_list_t)v8::External::Cast(*args[0])->Value();
kth_output_t elem = (kth_output_t)v8::External::Cast(*args[1])->Value();

kth_chain_output_list_push_back(list, elem);
}

void chain_output_list_destruct(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
throw_exception(isolate, "Wrong number of arguments. chain_output_list_destruct function requires 1 arguments.");
return;
}

void* vptr = v8::External::Cast(*args[0])->Value();
kth_output_list_t output_list = (kth_output_list_t)vptr;

void* vptr2 = v8::External::Cast(*args[1])->Value();
kth_output_t output = (kth_output_t)vptr2;
if ( ! args[0]->IsExternal()) {
throw_exception(isolate, "Wrong argument type for argument list (#1). Required to be IsExternal.");
return;
}

kth_chain_output_list_push_back(output_list, output);
kth_output_list_t list = (kth_output_list_t)v8::External::Cast(*args[0])->Value();

kth_chain_output_list_destruct(list);
}

void chain_output_list_count(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
throw_exception(isolate, "Wrong number of arguments");
throw_exception(isolate, "Wrong number of arguments. chain_output_list_count function requires 1 arguments.");
return;
}

if ( ! args[0]->IsExternal()) {
throw_exception(isolate, "Wrong arguments");
throw_exception(isolate, "Wrong argument type for argument list (#1). Required to be IsExternal.");
return;
}

void* vptr = v8::External::Cast(*args[0])->Value();
kth_output_list_t output_list = (kth_output_list_t)vptr;

uint64_t res = kth_chain_output_list_count(output_list);
kth_output_list_t list = (kth_output_list_t)v8::External::Cast(*args[0])->Value();

kth_size_t res = kth_chain_output_list_count(list);
args.GetReturnValue().Set(Number::New(isolate, res));
}

void chain_output_list_nth(v8::FunctionCallbackInfo<v8::Value> const& args) {
Isolate* isolate = args.GetIsolate();

if (args.Length() != 2) {
throw_exception(isolate, "Wrong number of arguments");
throw_exception(isolate, "Wrong number of arguments. chain_output_list_nth function requires 2 arguments.");
return;
}

if ( ! args[0]->IsExternal()) {
throw_exception(isolate, "Wrong arguments");
throw_exception(isolate, "Wrong argument type for argument list (#1). Required to be IsExternal.");
return;
}

if ( ! args[1]->IsNumber()) {
throw_exception(isolate, "Wrong arguments");
throw_exception(isolate, "Wrong argument type for argument n (#2). Required to be IsNumber.");
return;
}

void* vptr = v8::External::Cast(*args[0])->Value();
kth_output_list_t output_list = (kth_output_list_t)vptr;

uint64_t n = args[1]->IntegerValue(isolate->GetCurrentContext()).ToChecked();
kth_output_list_t list = (kth_output_list_t)v8::External::Cast(*args[0])->Value();
kth_size_t n = args[1]->IntegerValue(isolate->GetCurrentContext()).ToChecked();

kth_output_t res = kth_chain_output_list_nth(output_list, n);
kth_output_t res = kth_chain_output_list_nth(list, n);
args.GetReturnValue().Set(External::New(isolate, res));
}

Expand Down

0 comments on commit 3b23f8d

Please sign in to comment.