-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add int64_t support to embind using bigint support #13889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36220b6
3112759
fde8c15
bd842ba
f90457b
07794e3
65d3f21
74f8eef
ca8f4e9
a519f9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,8 @@ namespace emscripten { | |
EM_VAL _emval_get_property(EM_VAL object, EM_VAL key); | ||
void _emval_set_property(EM_VAL object, EM_VAL key, EM_VAL value); | ||
EM_GENERIC_WIRE_TYPE _emval_as(EM_VAL value, TYPEID returnType, EM_DESTRUCTORS* destructors); | ||
int64_t _emval_as_int64(EM_VAL value, TYPEID returnType); | ||
uint64_t _emval_as_uint64(EM_VAL value, TYPEID returnType); | ||
kripken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bool _emval_equals(EM_VAL first, EM_VAL second); | ||
bool _emval_strictly_equals(EM_VAL first, EM_VAL second); | ||
|
@@ -190,6 +192,7 @@ namespace emscripten { | |
const void* p; | ||
} w[2]; | ||
double d; | ||
uint64_t u; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need both signed and unsigned? i'd hope only unsigned is enough, as with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kripken You are correct, the |
||
}; | ||
static_assert(sizeof(GenericWireType) == 8, "GenericWireType must be 8 bytes"); | ||
static_assert(alignof(GenericWireType) == 8, "GenericWireType must be 8-byte-aligned"); | ||
|
@@ -204,6 +207,16 @@ namespace emscripten { | |
++cursor; | ||
} | ||
|
||
inline void writeGenericWireType(GenericWireType*& cursor, int64_t wt) { | ||
cursor->u = wt; | ||
++cursor; | ||
} | ||
|
||
inline void writeGenericWireType(GenericWireType*& cursor, uint64_t wt) { | ||
cursor->u = wt; | ||
++cursor; | ||
} | ||
|
||
template<typename T> | ||
void writeGenericWireType(GenericWireType*& cursor, T* wt) { | ||
cursor->w[0].p = wt; | ||
|
@@ -501,6 +514,30 @@ namespace emscripten { | |
return fromGenericWireType<T>(result); | ||
} | ||
|
||
template<> | ||
int64_t as<int64_t>() const { | ||
using namespace internal; | ||
|
||
typedef BindingType<int64_t> BT; | ||
typename WithPolicies<>::template ArgTypeList<int64_t> targetType; | ||
|
||
return _emval_as_int64( | ||
handle, | ||
targetType.getTypes()[0]); | ||
} | ||
|
||
template<> | ||
uint64_t as<uint64_t>() const { | ||
using namespace internal; | ||
|
||
typedef BindingType<uint64_t> BT; | ||
typename WithPolicies<>::template ArgTypeList<uint64_t> targetType; | ||
|
||
return _emval_as_uint64( | ||
handle, | ||
targetType.getTypes()[0]); | ||
} | ||
|
||
// If code is not being compiled with GNU extensions enabled, typeof() is not a reserved keyword, so support that as a member function. | ||
#if __STRICT_ANSI__ | ||
val typeof() const { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright 2021 The Emscripten Authors. All rights reserved. | ||
// Emscripten is available under two separate licenses, the MIT license and the | ||
// University of Illinois/NCSA Open Source License. Both these licenses can be | ||
// found in the LICENSE file. | ||
|
||
#include <stdio.h> | ||
#include <iostream> | ||
#include <cmath> | ||
#include <emscripten/bind.h> | ||
#include <emscripten/emscripten.h> | ||
#include <emscripten/val.h> | ||
|
||
using namespace emscripten; | ||
using namespace std; | ||
|
||
void fail() | ||
{ | ||
cout << "fail\n"; | ||
} | ||
|
||
void pass() | ||
{ | ||
cout << "pass\n"; | ||
} | ||
|
||
void test(string message) | ||
{ | ||
cout << "test:\n" << message << "\n"; | ||
} | ||
|
||
void ensure(bool value) | ||
{ | ||
if (value) | ||
pass(); | ||
else | ||
fail(); | ||
} | ||
|
||
void execute_js(string js_code) | ||
{ | ||
js_code.append(";"); | ||
const char* js_code_pointer = js_code.c_str(); | ||
EM_ASM_INT({ | ||
var js_code = UTF8ToString($0); | ||
return eval(js_code); | ||
}, js_code_pointer); | ||
} | ||
|
||
void ensure_js(string js_code) | ||
{ | ||
js_code.append(";"); | ||
const char* js_code_pointer = js_code.c_str(); | ||
ensure(EM_ASM_INT({ | ||
var js_code = UTF8ToString($0); | ||
return eval(js_code); | ||
}, js_code_pointer)); | ||
} | ||
|
||
void ensure_js_throws(string js_code, string error_type) | ||
{ | ||
js_code.append(";"); | ||
const char* js_code_pointer = js_code.c_str(); | ||
const char* error_type_pointer = error_type.c_str(); | ||
ensure(EM_ASM_INT({ | ||
var js_code = UTF8ToString($0); | ||
var error_type = UTF8ToString($1); | ||
try { | ||
eval(js_code); | ||
} | ||
catch(error_thrown) | ||
{ | ||
return error_thrown.name === error_type; | ||
} | ||
return false; | ||
}, js_code_pointer, error_type_pointer)); | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS(tests) { | ||
register_vector<int64_t>("Int64Vector"); | ||
register_vector<uint64_t>("UInt64Vector"); | ||
} | ||
|
||
int main() | ||
{ | ||
const int64_t max_int64_t = numeric_limits<int64_t>::max(); | ||
const int64_t min_int64_t = numeric_limits<int64_t>::min(); | ||
const uint64_t max_uint64_t = numeric_limits<uint64_t>::max(); | ||
|
||
printf("start\n"); | ||
|
||
test("vector<int64_t>"); | ||
val::global().set("v64", val(vector<int64_t>{1, 2, 3, -4})); | ||
ensure_js("v64.get(0) === 1n"); | ||
ensure_js("v64.get(1) === 2n"); | ||
ensure_js("v64.get(2) === 3n"); | ||
ensure_js("v64.get(3) === -4n"); | ||
|
||
execute_js("v64.push_back(1234n)"); | ||
ensure_js("v64.size() === 5"); | ||
ensure_js("v64.get(4) === 1234n"); | ||
|
||
test("vector<int64_t> Cannot convert number to int64_t"); | ||
ensure_js_throws("v64.push_back(1234)", "TypeError"); | ||
|
||
test("vector<int64_t> Cannot convert bigint that is too big"); | ||
ensure_js_throws("v64.push_back(12345678901234567890123456n)", "TypeError"); | ||
|
||
test("vector<uint64_t>"); | ||
val::global().set("vU64", val(vector<uint64_t>{1, 2, 3, 4})); | ||
ensure_js("vU64.get(0) === 1n"); | ||
ensure_js("vU64.get(1) === 2n"); | ||
ensure_js("vU64.get(2) === 3n"); | ||
ensure_js("vU64.get(3) === 4n"); | ||
|
||
execute_js("vU64.push_back(1234n)"); | ||
ensure_js("vU64.size() === 5"); | ||
ensure_js("vU64.get(4) === 1234n"); | ||
|
||
test("vector<uint64_t> Cannot convert number to uint64_t"); | ||
ensure_js_throws("vU64.push_back(1234)", "TypeError"); | ||
|
||
test("vector<uint64_t> Cannot convert bigint that is too big"); | ||
ensure_js_throws("vU64.push_back(12345678901234567890123456n)", "TypeError"); | ||
|
||
test("vector<uint64_t> Cannot convert bigint that is negative"); | ||
ensure_js_throws("vU64.push_back(-1n)", "TypeError"); | ||
|
||
printf("end\n"); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
start | ||
test: | ||
vector<int64_t> | ||
pass | ||
pass | ||
pass | ||
pass | ||
pass | ||
pass | ||
test: | ||
vector<int64_t> Cannot convert number to int64_t | ||
pass | ||
test: | ||
vector<int64_t> Cannot convert bigint that is too big | ||
pass | ||
test: | ||
vector<uint64_t> | ||
pass | ||
pass | ||
pass | ||
pass | ||
pass | ||
pass | ||
test: | ||
vector<uint64_t> Cannot convert number to uint64_t | ||
pass | ||
test: | ||
vector<uint64_t> Cannot convert bigint that is too big | ||
pass | ||
test: | ||
vector<uint64_t> Cannot convert bigint that is negative | ||
pass | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See issue #13902