VCVRack / VCV-Prototype

Other
130 stars 23 forks source link

What's going on here? #1

Closed JerrySievert closed 4 years ago

JerrySievert commented 4 years ago

https://github.com/VCVRack/VCV-Prototype/blob/master/src/DuktapeEngine.cpp#L114-L115

trying to figure out your intent for the QuickJS engine.

it looks like you're pushing an array of floats in as a direct memory buffer and looking to access that in javascript?

                var x = block.inputs[i][0]

if that's the case, this will fail depending on the endianness of the host, and appears to be a Duktape only extension. Are you working around the lack of Float32Array and Buffer?

I'm trying to figure out a good analog in QuickJS that will be compatible.

AndrewBelt commented 4 years ago

If QuickJS doesn't have something like that, the engine will undoubtedly be much slower than Duktape. But I'd be surprised if it didn't since setting external buffers is pretty much the entire point of ArrayBuffers in JavaScript. Isn't this what you need?

JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
                          JSFreeArrayBufferDataFunc *free_func, void *opaque,
                          JS_BOOL is_shared);

Endianness should have no effect because we're not casting between types with different sizes. We have a float buffer and want to expose it as a Float32Array.

JerrySievert commented 4 years ago
JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
                          JSFreeArrayBufferDataFunc *free_func, void *opaque,
                          JS_BOOL is_shared);

sort of - been down that path, it's an 8bit buffer which is what made me ask. Float32Array is supported, but what made me hesitate was how you were accessing:

                var x = block.inputs[i][0]

since in the code, it looks like you're allocating a DUK_BUFOBJ_FLOAT32ARRAY which should be one-dimensional, but you're accessing it in two dimensions, which is what made me pause and ask the intent.

> var a = new Float32Array(5);
undefined
> a[0][0]
undefined
> a[0]
0
> a[0] = 1
1
> a[0][0]
undefined
AndrewBelt commented 4 years ago

I'm making a normal Javascript array of Float32Arrays. Nothing magic is going on. In JS, it'd look like

var inputs = []
for (var i = 0; i < NUM_ROWS; i++)
    inputs[i] = new Float32Array(n);

If the above function does what I think it does, it should definitely be possible for Float32Arrays to be created in QuickJS from C. I haven't looked at the details, but it would severely cripple QuickJS's usefulness if it's impossible.

JerrySievert commented 4 years ago

ah, I see - I think what confused me is that there is only one row of inputs, which really does seem a bit odd given the corresponding c++ API, where inputs is a one-dimensional array.

AndrewBelt commented 4 years ago

It's two-dimensional of size (NUM_ROWS, bufferSize). See https://github.com/VCVRack/VCV-Prototype#scripting-api

JerrySievert commented 4 years ago

ah, thank you. that's what wasn't making sense.