-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
sea: use JSON configuration and blob content for SEA #47125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
47fe41a
sea: use JSON configuration and blob content for SEA
joyeecheung ce5c5e0
fixup! sea: use JSON configuration and blob content for SEA
joyeecheung c918777
fixup! sea: use JSON configuration and blob content for SEA
joyeecheung d8d7f13
fixup! fixup! sea: use JSON configuration and blob content for SEA
joyeecheung 58ec5e9
fixup! fixup! fixup! sea: use JSON configuration and blob content fo…
joyeecheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "json_parser.h" | ||
#include "node_errors.h" | ||
#include "node_v8_platform-inl.h" | ||
#include "util-inl.h" | ||
|
||
namespace node { | ||
using v8::ArrayBuffer; | ||
using v8::Context; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::Object; | ||
using v8::String; | ||
using v8::Value; | ||
|
||
static Isolate* NewIsolate(v8::ArrayBuffer::Allocator* allocator) { | ||
Isolate* isolate = Isolate::Allocate(); | ||
CHECK_NOT_NULL(isolate); | ||
per_process::v8_platform.Platform()->RegisterIsolate(isolate, | ||
uv_default_loop()); | ||
Isolate::CreateParams params; | ||
params.array_buffer_allocator = allocator; | ||
Isolate::Initialize(isolate, params); | ||
return isolate; | ||
} | ||
|
||
void JSONParser::FreeIsolate(Isolate* isolate) { | ||
per_process::v8_platform.Platform()->UnregisterIsolate(isolate); | ||
isolate->Dispose(); | ||
} | ||
|
||
JSONParser::JSONParser() | ||
: allocator_(ArrayBuffer::Allocator::NewDefaultAllocator()), | ||
isolate_(NewIsolate(allocator_.get())), | ||
handle_scope_(isolate_.get()), | ||
context_(isolate_.get(), Context::New(isolate_.get())), | ||
context_scope_(context_.Get(isolate_.get())) {} | ||
|
||
bool JSONParser::Parse(const std::string& content) { | ||
DCHECK(!parsed_); | ||
|
||
Isolate* isolate = isolate_.get(); | ||
Local<Context> context = context_.Get(isolate); | ||
|
||
// It's not a real script, so don't print the source line. | ||
errors::PrinterTryCatch bootstrapCatch( | ||
isolate, errors::PrinterTryCatch::kDontPrintSourceLine); | ||
Local<Value> json_string_value; | ||
Local<Value> result_value; | ||
if (!ToV8Value(context, content).ToLocal(&json_string_value) || | ||
!json_string_value->IsString() || | ||
!v8::JSON::Parse(context, json_string_value.As<String>()) | ||
.ToLocal(&result_value) || | ||
!result_value->IsObject()) { | ||
return false; | ||
} | ||
content_.Reset(isolate, result_value.As<Object>()); | ||
parsed_ = true; | ||
return true; | ||
} | ||
|
||
std::optional<std::string> JSONParser::GetTopLevelField( | ||
const std::string& field) { | ||
Isolate* isolate = isolate_.get(); | ||
Local<Context> context = context_.Get(isolate); | ||
Local<Object> content_object = content_.Get(isolate); | ||
Local<Value> value; | ||
// It's not a real script, so don't print the source line. | ||
errors::PrinterTryCatch bootstrapCatch( | ||
isolate, errors::PrinterTryCatch::kDontPrintSourceLine); | ||
if (!content_object | ||
->Get(context, OneByteString(isolate, field.c_str(), field.length())) | ||
.ToLocal(&value) || | ||
!value->IsString()) { | ||
return {}; | ||
} | ||
Utf8Value utf8_value(isolate, value); | ||
return utf8_value.ToString(); | ||
} | ||
|
||
} // namespace node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef SRC_JSON_PARSER_H_ | ||
#define SRC_JSON_PARSER_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include <string> | ||
#include "util.h" | ||
#include "v8.h" | ||
|
||
namespace node { | ||
// This is intended to be used to get some top-level fields out of a JSON | ||
// without having to spin up a full Node.js environment that unnecessarily | ||
// complicates things. | ||
class JSONParser { | ||
public: | ||
JSONParser(); | ||
~JSONParser() {} | ||
bool Parse(const std::string& content); | ||
std::optional<std::string> GetTopLevelField(const std::string& field); | ||
|
||
private: | ||
// We might want a lighter-weight JSON parser for this use case. But for now | ||
// using V8 is good enough. | ||
static void FreeIsolate(v8::Isolate* isolate); | ||
std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_; | ||
DeleteFnPtr<v8::Isolate, FreeIsolate> isolate_; | ||
v8::HandleScope handle_scope_; | ||
v8::Global<v8::Context> context_; | ||
v8::Context::Scope context_scope_; | ||
v8::Global<v8::Object> content_; | ||
bool parsed_ = false; | ||
}; | ||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_JSON_PARSER_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.