Skip to content

Commit

Permalink
Trying different approaches, such as exposing tapes (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizperes committed Apr 17, 2020
1 parent 398a38f commit 20b2ece
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 31 additions & 2 deletions simdjson/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ Napi::Value simdjsonnode::ParseWrapped(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string json = info[0].As<Napi::String>();
try {

dom::parser parser;
return makeJSONObject(env, parser.parse(json));

} catch (simdjson_error &error) {
Napi::Error::New(env, error_message(error.error())).ThrowAsJavaScriptException();
return env.Null();
Expand All @@ -141,9 +139,40 @@ Napi::Object simdjsonnode::LazyParseWrapped(const Napi::CallbackInfo& info) {
return result;
}

Napi::Object simdjsonnode::BuffersWithParseWrapped(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string json = info[0].As<Napi::String>();
dom::parser parser;
error_code error = parser.parse(json).error();
if (error) {
Napi::Error::New(env, error_message(error)).ThrowAsJavaScriptException();
return Napi::Object::New(env);
}
uint64_t *tapebuf = std::move(parser.doc.tape).release();
uint8_t *stringbuf = std::move(parser.doc.string_buf).release();
size_t tapebufsize = parser.current_loc;
size_t stringbufsize = parser.current_string_buf_loc - stringbuf;
std::cout << "ass " << tapebufsize;
Napi::ArrayBuffer tapebufobj = Napi::ArrayBuffer::New(env, static_cast<void *>(tapebuf), tapebufsize,
[](Napi::Env /*env*/, void * obj) {
uint64_t *o = static_cast<uint64_t *>(obj);
delete o;
});
Napi::ArrayBuffer stringbufobj = Napi::ArrayBuffer::New(env, static_cast<void *>(stringbuf), stringbufsize,
[](Napi::Env /*env*/, void * obj) {
uint8_t *o = static_cast<uint8_t *>(obj);
delete o;
});
Napi::Object result = Napi::Object::New(env);
result.Set("tapeBuffer", tapebufobj);
result.Set("stringBuffer", stringbufobj);
return result;
}

Napi::Object simdjsonnode::Init(Napi::Env env, Napi::Object exports) {
exports.Set("isValid", Napi::Function::New(env, simdjsonnode::IsValidWrapped));
exports.Set("parse", Napi::Function::New(env, simdjsonnode::ParseWrapped));
exports.Set("lazyParse", Napi::Function::New(env, simdjsonnode::LazyParseWrapped));
exports.Set("buffersWith", Napi::Function::New(env, simdjsonnode::BuffersWithParseWrapped));
return exports;
}
2 changes: 2 additions & 0 deletions simdjson/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ namespace simdjsonnode {
Napi::Value ValueForKeyPathWrapped(const Napi::CallbackInfo& info);
Napi::Value findKeyPath(Napi::Env env, std::vector<std::string> subpaths, dom::element pjh);

Napi::Object BuffersWithParseWrapped(const Napi::CallbackInfo& info);

Napi::Object Init(Napi::Env env, Napi::Object exports);
}

0 comments on commit 20b2ece

Please sign in to comment.