Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/embind/test_val_read_pointer.cpp
4150 views
1
#include <emscripten/bind.h>
2
#include <emscripten.h>
3
4
using namespace emscripten;
5
6
struct ValueObject {
7
ValueObject(): value(43) {};
8
int value;
9
};
10
11
struct ValueArray {
12
ValueArray(): x(44) {};
13
int x;
14
};
15
16
struct Foo {
17
Foo(): value(45) {};
18
int value;
19
};
20
21
enum Enum { ONE, TWO };
22
23
EMSCRIPTEN_BINDINGS(xxx) {
24
value_object<ValueObject>("ValueObject")
25
.field("value", &ValueObject::value);
26
27
value_array<ValueArray>("ValueArray")
28
.element(&ValueArray::x);
29
30
class_<Foo>("Foo")
31
.property("value", &Foo::value);
32
33
enum_<Enum>("Enum")
34
.value("ONE", ONE)
35
.value("TWO", TWO);
36
}
37
38
39
int main() {
40
EM_ASM(
41
globalThis["passthrough"] = (arg) => {
42
return arg;
43
};
44
globalThis["passthroughValueObject"] = (arg) => {
45
return arg.value;
46
};
47
globalThis["passthroughValueArray"] = (arg) => {
48
return arg[0];
49
};
50
globalThis["passthroughClass"] = (arg) => {
51
const value = arg.value;
52
arg.delete();
53
return value;
54
};
55
globalThis["passthroughMemoryView"] = (arg) => {
56
return arg[2];
57
};
58
);
59
60
// These tests execute all the various readValueFromPointer functions for each
61
// of the different binding types.
62
assert(val::global().call<bool>("passthrough", true));
63
assert(val::global().call<int>("passthrough", 42) == 42);
64
assert(val::global().call<double>("passthrough", 42.2) == 42.2);
65
ValueObject vo;
66
assert(val::global().call<int>("passthroughValueObject", vo) == 43);
67
ValueArray va;
68
assert(val::global().call<int>("passthroughValueArray", va) == 44);
69
Foo foo;
70
assert(val::global().call<int>("passthroughClass", foo) == 45);
71
assert(val::global().call<std::string>("passthrough", val("abc")) == "abc");
72
std::string str = "hello";
73
assert(val::global().call<std::string>("passthrough", str) == "hello");
74
std::wstring wstr = L"abc";
75
assert(val::global().call<std::wstring>("passthrough", wstr) == L"abc");
76
static const int data[] = {0, 1, 2};
77
assert(val::global().call<int>("passthroughMemoryView", typed_memory_view(3, data)) == 2);
78
assert(val::global().call<Enum>("passthrough", ONE) == ONE);
79
80
return 0;
81
}
82
83