Skip to content

Commit 5541ca8

Browse files
committed
Add Uint8Array subtype on Value
1 parent 9b0c78f commit 5541ca8

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

runtime/elem/Value.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace js
2929
using Object = std::map<String, Value>;
3030
using Array = std::vector<Value>;
3131
using Float32Array = std::vector<float>;
32+
using Uint8Array = std::vector<uint8_t>;
3233
using Function = std::function<Value(Array)>;
3334

3435
//==============================================================================
@@ -52,6 +53,7 @@ namespace js
5253
Value (String const& v) : var(v) {}
5354
Value (Array const& v) : var(v) {}
5455
Value (Float32Array const& v) : var(v) {}
56+
Value (Uint8Array const& v) : var(v) {}
5557
Value (Object const& v) : var(v) {}
5658
Value (Function const& v) : var(v) {}
5759

@@ -81,6 +83,7 @@ namespace js
8183
bool isString() const { return std::holds_alternative<String>(var); }
8284
bool isArray() const { return std::holds_alternative<Array>(var); }
8385
bool isFloat32Array() const { return std::holds_alternative<Float32Array>(var); }
86+
bool isUint8Array() const { return std::holds_alternative<Uint8Array>(var); }
8487
bool isObject() const { return std::holds_alternative<Object>(var); }
8588
bool isFunction() const { return std::holds_alternative<Function>(var); }
8689

@@ -94,11 +97,13 @@ namespace js
9497
// Object value getters
9598
Array const& getArray() const { return std::get<Array>(var); }
9699
Float32Array const& getFloat32Array() const { return std::get<Float32Array>(var); }
100+
Uint8Array const& getUint8Array() const { return std::get<Uint8Array>(var); }
97101
Object const& getObject() const { return std::get<Object>(var); }
98102
Function const& getFunction() const { return std::get<Function>(var); }
99103

100104
Array& getArray() { return std::get<Array>(var); }
101105
Float32Array& getFloat32Array() { return std::get<Float32Array>(var); }
106+
Uint8Array& getUint8Array() { return std::get<Uint8Array>(var); }
102107
Object& getObject() { return std::get<Object>(var); }
103108
Function& getFunction() { return std::get<Function>(var); }
104109

@@ -160,6 +165,24 @@ namespace js
160165
auto s = ss.str();
161166
return s.substr(0, s.size() - 2) + "]";
162167
}
168+
if (isUint8Array())
169+
{
170+
auto& ab = getUint8Array();
171+
std::stringstream ss;
172+
ss << "[";
173+
174+
for (size_t i = 0; i < std::min((size_t) 3, ab.size()); ++i)
175+
ss << std::to_string(ab[i]) << ", ";
176+
177+
if (ab.size() > 3)
178+
{
179+
ss << "...]";
180+
return ss.str();
181+
}
182+
183+
auto s = ss.str();
184+
return s.substr(0, s.size() - 2) + "]";
185+
}
163186
if (isObject())
164187
{
165188
std::stringstream ss;
@@ -191,6 +214,7 @@ namespace js
191214
Object,
192215
Array,
193216
Float32Array,
217+
Uint8Array,
194218
Function>;
195219

196220
VarType var;

wasm/Main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ class ElementaryAudioProcessor
281281
// mapping from emscripten::val to a simple std::vector.
282282
return elem::js::Value(convertJSArrayToNumberVector<float>(v));
283283
}
284+
if (v.instanceof(val::global("ArrayBuffer"))) {
285+
// For raw ArrayBuffer, we need to create a Uint8Array view first
286+
auto uint8View = val::global("Uint8Array").new_(v);
287+
return elem::js::Value(elem::js::Uint8Array(convertJSArrayToNumberVector<uint8_t>(uint8View)));
288+
}
289+
if (v.instanceof(val::global("Uint8Array"))) {
290+
// This conversion function is part of the emscripten namespace for
291+
// mapping from emscripten::val to a simple std::vector.
292+
return elem::js::Value(elem::js::Uint8Array(convertJSArrayToNumberVector<uint8_t>(v)));
293+
}
284294

285295
if (v.isArray())
286296
{
@@ -361,6 +371,21 @@ class ElementaryAudioProcessor
361371
return ret;
362372
}
363373

374+
if (v.isUint8Array())
375+
{
376+
auto& va = v.getUint8Array();
377+
378+
// Create a Uint8Array directly
379+
auto uint8Array = val::global("Uint8Array").new_(va.size());
380+
381+
for (size_t i = 0; i < va.size(); ++i)
382+
{
383+
uint8Array.set(i, val(va[i]));
384+
}
385+
386+
return uint8Array;
387+
}
388+
364389
if (v.isObject())
365390
{
366391
auto& vo = v.getObject();

0 commit comments

Comments
 (0)