Repository navigation
Expand file tree
/
Copy pathJSON.cpp
More file actions
76 lines (67 loc) · 2.06 KB
/
Copy pathJSON.cpp
File metadata and controls
76 lines (67 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
//===----------------------------------------------------------------------===//
/// \file
/// ES5.1 15.12 Populate the JSON object.
//===----------------------------------------------------------------------===//
#include "JSLibInternal.h"
#include "hermes/VM/JSLib/RuntimeJSONUtils.h"
#include "hermes/VM/SingleObject.h"
#include "hermes/VM/StringPrimitive.h"
namespace hermes {
namespace vm {
Handle<JSObject> createJSONObject(Runtime &runtime) {
auto objRes = JSJSON::create(
runtime, Handle<JSObject>::vmcast(&runtime.objectPrototype));
assert(
objRes != ExecutionStatus::EXCEPTION && "unable to define JSON object");
auto json = runtime.makeHandle<JSJSON>(*objRes);
defineMethod(
runtime,
json,
Predefined::getSymbolID(Predefined::parse),
nullptr,
jsonParse,
2);
defineMethod(
runtime,
json,
Predefined::getSymbolID(Predefined::stringify),
nullptr,
jsonStringify,
3);
auto dpf = DefinePropertyFlags::getDefaultNewPropertyFlags();
dpf.writable = 0;
dpf.enumerable = 0;
defineProperty(
runtime,
json,
Predefined::getSymbolID(Predefined::SymbolToStringTag),
runtime.getPredefinedStringHandle(Predefined::JSON),
dpf);
return json;
}
CallResult<HermesValue> jsonParse(void *, Runtime &runtime, NativeArgs args) {
auto res = toString_RJS(runtime, args.getArgHandle(0));
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return runtimeJSONParse(
runtime,
runtime.makeHandle(std::move(*res)),
Handle<Callable>::dyn_vmcast(args.getArgHandle(1)));
}
CallResult<HermesValue>
jsonStringify(void *, Runtime &runtime, NativeArgs args) {
return runtimeJSONStringify(
runtime,
args.getArgHandle(0),
args.getArgHandle(1),
args.getArgHandle(2));
}
} // namespace vm
} // namespace hermes