|
| 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 fail() |
| 17 | +{ |
| 18 | + cout << "fail\n"; |
| 19 | +} |
| 20 | + |
| 21 | +void pass() |
| 22 | +{ |
| 23 | + cout << "pass\n"; |
| 24 | +} |
| 25 | + |
| 26 | +void test(string message) |
| 27 | +{ |
| 28 | + cout << "test:\n" << message << "\n"; |
| 29 | +} |
| 30 | + |
| 31 | +void ensure(bool value) |
| 32 | +{ |
| 33 | + if (value) |
| 34 | + pass(); |
| 35 | + else |
| 36 | + fail(); |
| 37 | +} |
| 38 | + |
| 39 | +void execute_js(string js_code) |
| 40 | +{ |
| 41 | + js_code.append(";"); |
| 42 | + const char* js_code_pointer = js_code.c_str(); |
| 43 | + EM_ASM_INT({ |
| 44 | + var js_code = UTF8ToString($0); |
| 45 | + return eval(js_code); |
| 46 | + }, js_code_pointer); |
| 47 | +} |
| 48 | + |
| 49 | +void ensure_js(string js_code) |
| 50 | +{ |
| 51 | + js_code.append(";"); |
| 52 | + const char* js_code_pointer = js_code.c_str(); |
| 53 | + ensure(EM_ASM_INT({ |
| 54 | + var js_code = UTF8ToString($0); |
| 55 | + return eval(js_code); |
| 56 | + }, js_code_pointer)); |
| 57 | +} |
| 58 | + |
| 59 | +void ensure_js_throws(string js_code, string error_type) |
| 60 | +{ |
| 61 | + js_code.append(";"); |
| 62 | + const char* js_code_pointer = js_code.c_str(); |
| 63 | + const char* error_type_pointer = error_type.c_str(); |
| 64 | + ensure(EM_ASM_INT({ |
| 65 | + var js_code = UTF8ToString($0); |
| 66 | + var error_type = UTF8ToString($1); |
| 67 | + try { |
| 68 | + eval(js_code); |
| 69 | + } |
| 70 | + catch(error_thrown) |
| 71 | + { |
| 72 | + return error_thrown.name === error_type; |
| 73 | + } |
| 74 | + return false; |
| 75 | + }, js_code_pointer, error_type_pointer)); |
| 76 | +} |
| 77 | + |
| 78 | +EMSCRIPTEN_BINDINGS(tests) { |
| 79 | + register_vector<int64_t>("Int64Vector"); |
| 80 | + register_vector<uint64_t>("UInt64Vector"); |
| 81 | +} |
| 82 | + |
| 83 | +int main() |
| 84 | +{ |
| 85 | + const int64_t max_int64_t = numeric_limits<int64_t>::max(); |
| 86 | + const int64_t min_int64_t = numeric_limits<int64_t>::min(); |
| 87 | + const uint64_t max_uint64_t = numeric_limits<uint64_t>::max(); |
| 88 | + |
| 89 | + printf("start\n"); |
| 90 | + |
| 91 | + test("vector<int64_t>"); |
| 92 | + val::global().set("v64", val(vector<int64_t>{1, 2, 3, -4})); |
| 93 | + ensure_js("v64.get(0) === 1n"); |
| 94 | + ensure_js("v64.get(1) === 2n"); |
| 95 | + ensure_js("v64.get(2) === 3n"); |
| 96 | + ensure_js("v64.get(3) === -4n"); |
| 97 | + |
| 98 | + execute_js("v64.push_back(1234n)"); |
| 99 | + ensure_js("v64.size() === 5"); |
| 100 | + ensure_js("v64.get(4) === 1234n"); |
| 101 | + |
| 102 | + test("vector<int64_t> Cannot convert number to int64_t"); |
| 103 | + ensure_js_throws("v64.push_back(1234)", "TypeError"); |
| 104 | + |
| 105 | + test("vector<int64_t> Cannot convert bigint that is too big"); |
| 106 | + ensure_js_throws("v64.push_back(12345678901234567890123456n)", "TypeError"); |
| 107 | + |
| 108 | + test("vector<uint64_t>"); |
| 109 | + val::global().set("vU64", val(vector<uint64_t>{1, 2, 3, 4})); |
| 110 | + ensure_js("vU64.get(0) === 1n"); |
| 111 | + ensure_js("vU64.get(1) === 2n"); |
| 112 | + ensure_js("vU64.get(2) === 3n"); |
| 113 | + ensure_js("vU64.get(3) === 4n"); |
| 114 | + |
| 115 | + execute_js("vU64.push_back(1234n)"); |
| 116 | + ensure_js("vU64.size() === 5"); |
| 117 | + ensure_js("vU64.get(4) === 1234n"); |
| 118 | + |
| 119 | + test("vector<uint64_t> Cannot convert number to uint64_t"); |
| 120 | + ensure_js_throws("vU64.push_back(1234)", "TypeError"); |
| 121 | + |
| 122 | + test("vector<uint64_t> Cannot convert bigint that is too big"); |
| 123 | + ensure_js_throws("vU64.push_back(12345678901234567890123456n)", "TypeError"); |
| 124 | + |
| 125 | + test("vector<uint64_t> Cannot convert bigint that is negative"); |
| 126 | + ensure_js_throws("vU64.push_back(-1n)", "TypeError"); |
| 127 | + |
| 128 | + printf("end\n"); |
| 129 | + return 0; |
| 130 | +} |
0 commit comments