Path: blob/main/test/embind/test_i64_binding.cpp
4150 views
// Copyright 2021 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45#include <stdio.h>6#include <iostream>7#include <cmath>8#include <emscripten/bind.h>9#include <emscripten/emscripten.h>10#include <emscripten/val.h>1112using namespace emscripten;13using namespace std;1415void test(string message)16{17printf("test: %s\n", message.c_str());18}1920void assert_js_eq(string X, string Y) {21string js_code;22js_code += "const x = " + X + ";";23js_code += "const y = " + Y + ";";24js_code += "assert(x === y, `" + X + ": actual = ${typeof x} ${x}, expected = ${typeof y} ${y}`);";25emscripten_run_script(js_code.c_str());26}2728EMSCRIPTEN_BINDINGS(tests) {29emscripten::function("int64_min", &numeric_limits<int64_t>::min);30emscripten::function("int64_max", &numeric_limits<int64_t>::max);31emscripten::function("uint64_max", &numeric_limits<uint64_t>::max);3233register_vector<int64_t>("Int64Vector");34register_vector<uint64_t>("UInt64Vector");35}3637extern "C" void ensure_js_throws_with_assertions_enabled(const char* js_code, const char* error_type);3839int main()40{41test("limits");4243assert_js_eq("Module.int64_min()", to_string(numeric_limits<int64_t>::min()) + "n");44assert_js_eq("Module.int64_max()", to_string(numeric_limits<int64_t>::max()) + "n");45assert_js_eq("Module.uint64_max()", to_string(numeric_limits<uint64_t>::max()) + "n");4647printf("start\n");4849test("vector<int64_t>");50val myval(std::vector<int64_t>{1, 2, 3, -4});51val::global().set("v64", myval);52assert_js_eq("v64.get(0)", "1n");53assert_js_eq("v64.get(1)", "2n");54assert_js_eq("v64.get(2)", "3n");55assert_js_eq("v64.get(3)", "-4n");5657emscripten_run_script("v64.push_back(1234n)");58assert_js_eq("v64.size()", "5");59assert_js_eq("v64.get(4)", "1234n");6061test("vector<int64_t> Cannot convert bigint that is too big");62ensure_js_throws_with_assertions_enabled("v64.push_back(12345678901234567890123456n)", "TypeError");6364test("vector<uint64_t>");65val myval2(vector<uint64_t>{1, 2, 3, 4});66val::global().set("vU64", myval2);67assert_js_eq("vU64.get(0)", "1n");68assert_js_eq("vU64.get(1)", "2n");69assert_js_eq("vU64.get(2)", "3n");70assert_js_eq("vU64.get(3)", "4n");7172emscripten_run_script("vU64.push_back(1234n)");73assert_js_eq("vU64.size()", "5");74assert_js_eq("vU64.get(4)", "1234n");7576emscripten_run_script("vU64.push_back(1234)");77assert_js_eq("vU64.size()", "6");78assert_js_eq("vU64.get(5)", "1234n");7980test("vector<uint64_t> Cannot convert bigint that is too big");81ensure_js_throws_with_assertions_enabled("vU64.push_back(12345678901234567890123456n)", "TypeError");8283test("vector<uint64_t> Cannot convert bigint that is negative");84ensure_js_throws_with_assertions_enabled("vU64.push_back(-1n)", "TypeError");8586myval.call<void>("delete");87myval2.call<void>("delete");88printf("end\n");8990return 0;91}929394