Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/embind/test_i64_binding.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 <cmath>
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
{
18
printf("test: %s\n", message.c_str());
19
}
20
21
void assert_js_eq(string X, string Y) {
22
string js_code;
23
js_code += "const x = " + X + ";";
24
js_code += "const y = " + Y + ";";
25
js_code += "assert(x === y, `" + X + ": actual = ${typeof x} ${x}, expected = ${typeof y} ${y}`);";
26
emscripten_run_script(js_code.c_str());
27
}
28
29
EMSCRIPTEN_BINDINGS(tests) {
30
emscripten::function("int64_min", &numeric_limits<int64_t>::min);
31
emscripten::function("int64_max", &numeric_limits<int64_t>::max);
32
emscripten::function("uint64_max", &numeric_limits<uint64_t>::max);
33
34
register_vector<int64_t>("Int64Vector");
35
register_vector<uint64_t>("UInt64Vector");
36
}
37
38
extern "C" void ensure_js_throws_with_assertions_enabled(const char* js_code, const char* error_type);
39
40
int main()
41
{
42
test("limits");
43
44
assert_js_eq("Module.int64_min()", to_string(numeric_limits<int64_t>::min()) + "n");
45
assert_js_eq("Module.int64_max()", to_string(numeric_limits<int64_t>::max()) + "n");
46
assert_js_eq("Module.uint64_max()", to_string(numeric_limits<uint64_t>::max()) + "n");
47
48
printf("start\n");
49
50
test("vector<int64_t>");
51
val myval(std::vector<int64_t>{1, 2, 3, -4});
52
val::global().set("v64", myval);
53
assert_js_eq("v64.get(0)", "1n");
54
assert_js_eq("v64.get(1)", "2n");
55
assert_js_eq("v64.get(2)", "3n");
56
assert_js_eq("v64.get(3)", "-4n");
57
58
emscripten_run_script("v64.push_back(1234n)");
59
assert_js_eq("v64.size()", "5");
60
assert_js_eq("v64.get(4)", "1234n");
61
62
test("vector<int64_t> Cannot convert bigint that is too big");
63
ensure_js_throws_with_assertions_enabled("v64.push_back(12345678901234567890123456n)", "TypeError");
64
65
test("vector<uint64_t>");
66
val myval2(vector<uint64_t>{1, 2, 3, 4});
67
val::global().set("vU64", myval2);
68
assert_js_eq("vU64.get(0)", "1n");
69
assert_js_eq("vU64.get(1)", "2n");
70
assert_js_eq("vU64.get(2)", "3n");
71
assert_js_eq("vU64.get(3)", "4n");
72
73
emscripten_run_script("vU64.push_back(1234n)");
74
assert_js_eq("vU64.size()", "5");
75
assert_js_eq("vU64.get(4)", "1234n");
76
77
emscripten_run_script("vU64.push_back(1234)");
78
assert_js_eq("vU64.size()", "6");
79
assert_js_eq("vU64.get(5)", "1234n");
80
81
test("vector<uint64_t> Cannot convert bigint that is too big");
82
ensure_js_throws_with_assertions_enabled("vU64.push_back(12345678901234567890123456n)", "TypeError");
83
84
test("vector<uint64_t> Cannot convert bigint that is negative");
85
ensure_js_throws_with_assertions_enabled("vU64.push_back(-1n)", "TypeError");
86
87
myval.call<void>("delete");
88
myval2.call<void>("delete");
89
printf("end\n");
90
91
return 0;
92
}
93
94