-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathvalue.cpp
More file actions
85 lines (63 loc) · 2.19 KB
/
Copy pathvalue.cpp
File metadata and controls
85 lines (63 loc) · 2.19 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
77
78
79
80
81
82
83
84
85
#include "quickjspp.hpp"
#include <iostream>
#include <cstring>
int main()
{
qjs::Runtime runtime;
qjs::Context context(runtime);
try
{
auto val1 = context.newValue(321);
auto val2 = context.newValue(123);
assert(val1 != val2);
val1 = val2;
assert(val1 == val2);
val1 = context.newValue(123);
assert(val1 == val2);
val1 = std::move(val2);
assert(val1.as<std::string>() == "123");
assert((double) val1 == 123.0);
val2 = context.newValue((std::string) "123");
assert(val1 != val2);
context.global()["val1"] = val1;
context.global()["val2"] = val2;
assert((bool) context.eval("val1 !== val2"));
assert((bool) context.eval("val1 == val2"));
//
val1 = context.newObject();
val1["a"] = "1";
val2 = context.newObject();
val2["a"] = "1";
assert(val1 != val2);
context.global()["val1"] = val1;
context.global()["val2"] = val2;
assert((bool) context.eval("val1 !== val2"));
assert((bool) context.eval("val1 != val2"));
assert((bool)context.eval("JSON.stringify(val1) === JSON.stringify(val2)"));
qjs::Value one = val1["a"];
assert((int)one == 1);
assert(val1.toJSON() == val2.toJSON());
val1["b"] = context.newObject();
val1["b"][2] = 2;
assert((int)val1["b"][2] == 2);
assert(val1.toJSON() == context.fromJSON(val1.toJSON(JS_UNDEFINED, context.newValue("\t"))).toJSON());
assert(val1.toJSON() == "{\"a\":\"1\",\"b\":{\"2\":2}}");
assert(val1 == context.global()["val1"]);
auto str = val1.toJSON();
assert(str == (std::string) context.eval(R"xxx(
val1 = new Object();
val1["a"] = "1";
val1["b"] = new Object();
val1["b"][2] = 2;
JSON.stringify(val1)
)xxx"));
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string) exc << std::endl;
if((bool) exc["stack"])
std::cerr << (std::string) exc["stack"] << std::endl;
return 1;
}
}