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