Path: blob/main/test/embind/test_i64_val.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 <sstream>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) {16cout << "test:\n" << message << "\n";17}1819void ensure_js(string js_code) {20js_code.append(";");21const char* js_code_pointer = js_code.c_str();22assert(EM_ASM_INT({23var js_code = UTF8ToString($0);24return eval(js_code);25}, js_code_pointer));26}2728template <typename T>29string compare_a_64_js(T value) {30stringstream ss;31ss << "a === " << value << "n";32return ss.str();33}3435template <typename T>36void test_value(T value) {37cout << " testing value " << value << endl;38cout << " setting properties preserves the expected value" << endl;39val::global().set("a", val(value));40ensure_js(compare_a_64_js(value));41cout << " getting properties returns the original value intact" << endl;42assert(val::global()["a"].as<T>() == value);43cout << " function calls roundtrip the value correctly" << endl;44assert(val::global("BigInt")(value).template as<T>() == value);45cout << " method calls roundtrip the value correctly" << endl;46assert(val::global().call<T>("BigInt", value) == value);47}4849int main() {50const int64_t max_int64_t = numeric_limits<int64_t>::max();51const int64_t min_int64_t = numeric_limits<int64_t>::min();52const uint64_t max_uint64_t = numeric_limits<uint64_t>::max();53std::array<std::uint64_t, 5> uint64Array = {0, 1, 2, 3, 4};54std::array<std::int64_t, 5> int64Array = {-2, -1, 0, 1, 2};5556printf("start\n");57EM_ASM({globalThis.a = null});5859test("val(uint64_t v)");60test_value(uint64_t(1234));61test_value(max_uint64_t);6263test("val(int64_t v)");64test_value(int64_t(1234));65test_value(int64_t(-4321));66test_value(int64_t(0x12345678aabbccddL));67test_value(min_int64_t);68test_value(max_int64_t);6970test("val(typed_memory_view<uint64_t>)");71val::global().set("a", val(typed_memory_view(uint64Array.size(), uint64Array.data())));72ensure_js("a instanceof BigUint64Array");73ensure_js("a.length === 5");74ensure_js("a[0] === 0n");75ensure_js("a[4] === 4n");7677test("val(typed_memory_view<int64_t>)");78val::global().set("a", val(typed_memory_view(int64Array.size(), int64Array.data())));79ensure_js("a instanceof BigInt64Array");80ensure_js("a.length === 5");81ensure_js("a[0] === -2n");82ensure_js("a[4] === 2n");8384printf("end\n");85return 0;86}878889