Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/embind/test_i64_val.cpp
4150 views
1
// Copyright 2021 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <stdio.h>
7
#include <iostream>
8
#include <sstream>
9
#include <emscripten/bind.h>
10
#include <emscripten/emscripten.h>
11
#include <emscripten/val.h>
12
13
using namespace emscripten;
14
using namespace std;
15
16
void test(string message) {
17
cout << "test:\n" << message << "\n";
18
}
19
20
void ensure_js(string js_code) {
21
js_code.append(";");
22
const char* js_code_pointer = js_code.c_str();
23
assert(EM_ASM_INT({
24
var js_code = UTF8ToString($0);
25
return eval(js_code);
26
}, js_code_pointer));
27
}
28
29
template <typename T>
30
string compare_a_64_js(T value) {
31
stringstream ss;
32
ss << "a === " << value << "n";
33
return ss.str();
34
}
35
36
template <typename T>
37
void test_value(T value) {
38
cout << " testing value " << value << endl;
39
cout << " setting properties preserves the expected value" << endl;
40
val::global().set("a", val(value));
41
ensure_js(compare_a_64_js(value));
42
cout << " getting properties returns the original value intact" << endl;
43
assert(val::global()["a"].as<T>() == value);
44
cout << " function calls roundtrip the value correctly" << endl;
45
assert(val::global("BigInt")(value).template as<T>() == value);
46
cout << " method calls roundtrip the value correctly" << endl;
47
assert(val::global().call<T>("BigInt", value) == value);
48
}
49
50
int main() {
51
const int64_t max_int64_t = numeric_limits<int64_t>::max();
52
const int64_t min_int64_t = numeric_limits<int64_t>::min();
53
const uint64_t max_uint64_t = numeric_limits<uint64_t>::max();
54
std::array<std::uint64_t, 5> uint64Array = {0, 1, 2, 3, 4};
55
std::array<std::int64_t, 5> int64Array = {-2, -1, 0, 1, 2};
56
57
printf("start\n");
58
EM_ASM({globalThis.a = null});
59
60
test("val(uint64_t v)");
61
test_value(uint64_t(1234));
62
test_value(max_uint64_t);
63
64
test("val(int64_t v)");
65
test_value(int64_t(1234));
66
test_value(int64_t(-4321));
67
test_value(int64_t(0x12345678aabbccddL));
68
test_value(min_int64_t);
69
test_value(max_int64_t);
70
71
test("val(typed_memory_view<uint64_t>)");
72
val::global().set("a", val(typed_memory_view(uint64Array.size(), uint64Array.data())));
73
ensure_js("a instanceof BigUint64Array");
74
ensure_js("a.length === 5");
75
ensure_js("a[0] === 0n");
76
ensure_js("a[4] === 4n");
77
78
test("val(typed_memory_view<int64_t>)");
79
val::global().set("a", val(typed_memory_view(int64Array.size(), int64Array.data())));
80
ensure_js("a instanceof BigInt64Array");
81
ensure_js("a.length === 5");
82
ensure_js("a[0] === -2n");
83
ensure_js("a[4] === 2n");
84
85
printf("end\n");
86
return 0;
87
}
88
89