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
6162 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_number_or_bigint_js(T value) {
31
static_assert(sizeof(T) == 8 || sizeof(T) == 4);
32
stringstream ss;
33
ss << "a === " << value;
34
if constexpr (sizeof(T) == 8) {
35
ss << "n";
36
}
37
return ss.str();
38
}
39
40
template <typename T>
41
void test_value(T value) {
42
static_assert(sizeof(T) == 8 || sizeof(T) == 4);
43
cout << " testing value " << value << endl;
44
cout << " setting properties preserves the expected value" << endl;
45
val::global().set("a", val(value));
46
ensure_js(compare_a_number_or_bigint_js(value));
47
cout << " getting properties returns the original value intact" << endl;
48
assert(val::global()["a"].as<T>() == value);
49
cout << " function calls roundtrip the value correctly" << endl;
50
const char* typeName;
51
if constexpr (sizeof(T) == 8) {
52
typeName = "BigInt";
53
} else {
54
typeName = "Number";
55
}
56
assert(val::global(typeName)(value).template as<T>() == value);
57
cout << " method calls roundtrip the value correctly" << endl;
58
assert(val::global().call<T>(typeName, value) == value);
59
}
60
61
int main() {
62
const int64_t max_int64_t = numeric_limits<int64_t>::max();
63
const int64_t min_int64_t = numeric_limits<int64_t>::min();
64
const uint64_t max_uint64_t = numeric_limits<uint64_t>::max();
65
const size_t max_size_t = std::numeric_limits<size_t>::max();
66
std::array<std::uint64_t, 5> uint64Array = {0, 1, 2, 3, 4};
67
std::array<std::int64_t, 5> int64Array = {-2, -1, 0, 1, 2};
68
69
printf("start\n");
70
EM_ASM({globalThis.a = null});
71
72
test("val(uint64_t v)");
73
test_value(uint64_t(1234));
74
test_value(max_uint64_t);
75
76
test("val(int64_t v)");
77
test_value(int64_t(1234));
78
test_value(int64_t(-4321));
79
test_value(int64_t(0x12345678aabbccddL));
80
test_value(min_int64_t);
81
test_value(max_int64_t);
82
83
test("val(size_t v)");
84
test_value(size_t(1234));
85
test_value(max_size_t);
86
87
test("val(typed_memory_view<uint64_t>)");
88
val::global().set("a", val(typed_memory_view(uint64Array.size(), uint64Array.data())));
89
ensure_js("a instanceof BigUint64Array");
90
ensure_js("a.length === 5");
91
ensure_js("a[0] === 0n");
92
ensure_js("a[4] === 4n");
93
94
test("val(typed_memory_view<int64_t>)");
95
val::global().set("a", val(typed_memory_view(int64Array.size(), int64Array.data())));
96
ensure_js("a instanceof BigInt64Array");
97
ensure_js("a.length === 5");
98
ensure_js("a[0] === -2n");
99
ensure_js("a[4] === 2n");
100
101
printf("end\n");
102
return 0;
103
}
104
105