This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chakrashim: add shim for JSON methods
Add v8::JSON::Parse and v8::JSON::Stringify to chakrashim. Also fixes some cpplint errors in v8.h and adds v8.h to the set of files we lint in vcbuild.bat. Fix the MacOS build to make sure that all chakrashim exports are re-exported by node. PR-URL: #313 Reviewed-By: Kunal Pathak <kunal.pathak@microsoft.com> Reviewed-By: Kyle Farnung <kfarnung@microsoft.com> Reviewed-By: Jimmy Thomson <jithomso@microsoft.com>
- Loading branch information
Showing
12 changed files
with
206 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,69 @@ | ||
// Copyright Microsoft. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files(the "Software"), to | ||
// deal in the Software without restriction, including without limitation the | ||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and / or | ||
// sell copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions : | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
// IN THE SOFTWARE. | ||
|
||
#include "v8chakra.h" | ||
|
||
namespace v8 { | ||
Local<Value> JSON::Parse(Local<String> json_string) { | ||
v8::Isolate* iso = jsrt::IsolateShim::GetCurrentAsIsolate(); | ||
return FromMaybe(JSON::Parse(iso, json_string)); | ||
} | ||
|
||
MaybeLocal<Value> JSON::Parse(Isolate* isolate, | ||
Local<String> json_string) { | ||
Local<Context> context = isolate->GetCurrentContext(); | ||
return JSON::Parse(context, json_string); | ||
} | ||
|
||
MaybeLocal<Value> JSON::Parse(Local<Context> context, | ||
Local<String> json_string) { | ||
jsrt::ContextShim *contextShim = jsrt::IsolateShim::GetContextShim( | ||
(JsContextRef)*context); | ||
|
||
JsValueRef jsonParseFunction = contextShim->GetjsonParseFunction(); | ||
JsValueRef obj = JS_INVALID_REFERENCE; | ||
|
||
if (jsrt::CallFunction(jsonParseFunction, (JsValueRef)*json_string, | ||
&obj) != JsNoError) { | ||
return Local<Value>(); | ||
} | ||
|
||
return Local<Value>::New(obj); | ||
} | ||
|
||
MaybeLocal<String> JSON::Stringify(Local<Context> context, | ||
Local<Object> json_object, | ||
Local<String> gap) { | ||
jsrt::ContextShim *contextShim = jsrt::IsolateShim::GetContextShim( | ||
(JsContextRef)*context); | ||
|
||
JsValueRef jsonStringifyFunction = contextShim->GetjsonStringifyFunction(); | ||
JsValueRef str = JS_INVALID_REFERENCE; | ||
JsValueRef actualGap = | ||
gap.IsEmpty() ? jsrt::GetUndefined() : static_cast<JsValueRef>(*gap); | ||
|
||
if (jsrt::CallFunction(jsonStringifyFunction, (JsValueRef)*json_object, | ||
jsrt::GetUndefined(), actualGap, &str) != JsNoError) { | ||
return Local<String>(); | ||
} | ||
|
||
return Local<String>::New(str); | ||
} | ||
} // namespace v8 |
This file contains 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 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,26 @@ | ||
#include <node.h> | ||
#include <v8.h> | ||
|
||
namespace { | ||
|
||
void Stringify(const v8::FunctionCallbackInfo<v8::Value>& args) { | ||
auto result = v8::JSON::Stringify(args.GetIsolate()->GetCurrentContext(), | ||
args[0].As<v8::Object>(), | ||
args[1].As<v8::String>()); | ||
args.GetReturnValue().Set(result.ToLocalChecked()); | ||
} | ||
|
||
void Parse(const v8::FunctionCallbackInfo<v8::Value>& args) { | ||
auto result = v8::JSON::Parse(args.GetIsolate()->GetCurrentContext(), | ||
args[0].As<v8::String>()); | ||
args.GetReturnValue().Set(result.ToLocalChecked()); | ||
} | ||
|
||
inline void Initialize(v8::Local<v8::Object> binding) { | ||
NODE_SET_METHOD(binding, "stringify", Stringify); | ||
NODE_SET_METHOD(binding, "parse", Parse); | ||
} | ||
|
||
NODE_MODULE(binding, Initialize) | ||
|
||
} // anonymous namespace |
This file contains 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,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], | ||
'sources': [ 'binding.cc' ] | ||
} | ||
] | ||
} |
Oops, something went wrong.