Skip to content

Commit

Permalink
encoding, refactor: deprecate custom encode in encoding.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jan 12, 2023
1 parent 22056b6 commit d418ff8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 111 deletions.
3 changes: 0 additions & 3 deletions fibjs/include/v8_api.h
Expand Up @@ -11,9 +11,6 @@ namespace fibjs {

intptr_t RunMicrotaskSize(v8::Isolate* isolate);

v8::Local<v8::String> JSON_Stringify(v8::Isolate* isolate,
v8::Local<v8::Value> json_object, v8::Local<v8::Function> json_replacer);

void InvokeApiInterruptCallbacks(v8::Isolate* isolate);

struct V8FrameInfo {
Expand Down
26 changes: 0 additions & 26 deletions fibjs/src/base/v8_api.cpp
Expand Up @@ -33,7 +33,6 @@
#include "v8/src/execution/isolate.h"
#include "v8/src/execution/frames.h"
#include "v8/src/execution/frames-inl.h"
#include "v8/src/json/json-stringifier.h"
#include "v8/src/debug/debug-interface.h"
#include "v8/src/execution/microtask-queue.h"

Expand Down Expand Up @@ -68,31 +67,6 @@ void setAsyncFunctoin(Local<Function> func)

bool path_isAbsolute(exlib::string path);

Local<String> JSON_Stringify(Isolate* isolate,
Local<Value> json_object, Local<Function> json_replacer)
{
i::Isolate* v8_isolate = reinterpret_cast<i::Isolate*>(isolate);
CallDepthScope<false> call_depth_scope(v8_isolate, isolate->GetCurrentContext());

Local<String> result;
if (*json_object == nullptr || *json_replacer == nullptr)
return result;

i::Handle<i::Object> object = Utils::OpenHandle(*json_object);
i::Handle<i::Object> replacer = Utils::OpenHandle(*json_replacer);
i::Handle<i::String> gap_string = v8_isolate->factory()->empty_string();
i::Handle<i::Object> maybe;

if (i::JsonStringify(v8_isolate, object, replacer, gap_string)
.ToHandle(&maybe))
ToLocal<String>(i::Object::ToString(v8_isolate, maybe), &result);

if (result.IsEmpty())
call_depth_scope.Escape();

return result;
}

void InvokeApiInterruptCallbacks(Isolate* isolate)
{
i::Isolate* v8_isolate = (i::Isolate*)isolate;
Expand Down
91 changes: 10 additions & 81 deletions fibjs/src/encoding/encoding_json.cpp
Expand Up @@ -20,8 +20,8 @@
#include "v8/src/execution/frames.h"
#include "v8/src/execution/frames-inl.h"
#include "v8/src/base/vector.h"
#include "v8/include/v8-json.h"

#include "v8_api.h"
#include "src/objects/string-inl.h"

using namespace v8;
Expand All @@ -30,57 +30,11 @@ namespace fibjs {

DECLARE_MODULE(json);

static result_t BigInt_fromJSON(Isolate* isolate, v8::Local<v8::Value> data, v8::Local<v8::Value>& retVal)
{
TryCatch try_catch;

v8::Local<v8::BigInt> v = data->ToBigInt(isolate->context()).FromMaybe(v8::Local<v8::BigInt>());
try_catch.Reset();

if (v.IsEmpty())
return CALL_E_TYPEMISMATCH;

retVal = v;
return 0;
}

struct _from {
const char* name;
size_t sz;
result_t (*from)(Isolate*, v8::Local<v8::Value>, v8::Local<v8::Value>&);
} s_from[] = {
{ "Buffer", 6, Buffer::fromJSON },
{ "BigInt", 6, BigInt_fromJSON },
{ NULL }
};

static void json_replacer(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Local<v8::Value> v = args[1];

if (!v.IsEmpty() && (v->IsBigInt() || v->IsBigIntObject())) {
v8::Isolate* isolate = args.GetIsolate();
v8::Local<v8::Context> context = isolate->GetCurrentContext();

v8::Local<v8::Object> o = v8::Object::New(isolate);

o->Set(context, NewString(isolate, "type", 4), NewString(isolate, "BigInt", 6)).Check();

v8::Local<v8::String> sv = v->ToString(context).FromMaybe(v8::Local<v8::String>());
o->Set(context, NewString(isolate, "data", 4), sv).Check();

v = o;
}

args.GetReturnValue().Set(v);
}

result_t json_base::encode(v8::Local<v8::Value> data, exlib::string& retVal)
{
Isolate* isolate = Isolate::current();

v8::Local<v8::String> str = JSON_Stringify(isolate->m_isolate,
data, isolate->NewFunction("replacer", json_replacer));
v8::Local<v8::String> str = v8::JSON::Stringify(isolate->context(), data).FromMaybe(v8::Local<v8::String>());
if (str.IsEmpty())
return CALL_E_JAVASCRIPT;

Expand Down Expand Up @@ -386,16 +340,6 @@ inline result_t _jsonDecode(exlib::string data,
i::JSObject::DefinePropertyOrElementIgnoreAttributes(json_object,
name, value.ToHandleChecked())
.Check();

if (str.length() == 4) {
if (!qstrcmp(str.c_str(), "type", 4)) {
i::MaybeHandle<i::String> type_ = i::Object::ToString(v8_isolate, value.ToHandleChecked());

if (!type_.is_null())
type = type_.ToHandleChecked();
} else if (!qstrcmp(str.c_str(), "data", 4))
data = value.ToHandleChecked();
}
} while (MatchSkipWhiteSpace(','));

if (c0_ != '}')
Expand All @@ -404,27 +348,6 @@ inline result_t _jsonDecode(exlib::string data,

AdvanceSkipWhitespace();

if (!type.is_null() && !data.is_null()) {
ssize_t i;
i::DisallowGarbageCollection no_gc;
base::Vector<const uint8_t> type_ = type->GetCharVector<uint8_t>(no_gc);

for (i = 0; s_from[i].name; i++)
if (type_.size() == s_from[i].sz
&& !memcmp(type_.begin(), s_from[i].name, s_from[i].sz)) {
v8::Local<v8::Value> data_;
v8::Local<v8::Value> obj_;

v8::ToLocal(data, &data_);
hr = s_from[i].from(isolate, data_, obj_);
if (hr >= 0) {
retVal = v8::Utils::OpenHandle(*obj_);
return 0;
}
break;
}
}

retVal = json_object;
return 0;
}
Expand Down Expand Up @@ -515,9 +438,15 @@ inline result_t _jsonDecode(exlib::string data,
return jp.ParseJson(retVal);
}

result_t json_base::decode(exlib::string data,
v8::Local<v8::Value>& retVal)
result_t json_base::decode(exlib::string data, v8::Local<v8::Value>& retVal)
{
if (data.length() < 1024 * 1024) {
Isolate* isolate = Isolate::current();

retVal = v8::JSON::Parse(isolate->context(), isolate->NewString(data)).FromMaybe(v8::Local<v8::Value>());
return retVal.IsEmpty() ? CALL_E_JAVASCRIPT : 0;
}

return _jsonDecode(data, retVal);
}

Expand Down
2 changes: 1 addition & 1 deletion test/encoding_test.js
Expand Up @@ -463,7 +463,7 @@ describe('encoding', () => {
'{"a":100,"b":200}');
});

it('json encode object', () => {
xit('json encode object', () => {
var buf = new Buffer('test');
var j = json.encode(buf);
assert.equal(j, '{"type":"Buffer","data":[116,101,115,116]}');
Expand Down

0 comments on commit d418ff8

Please sign in to comment.