Skip to content

Instantly share code, notes, and snippets.

@nikki93
Created October 6, 2016 08:16
Show Gist options
  • Save nikki93/65ea05c8eeb1172b649bbb0c9c057606 to your computer and use it in GitHub Desktop.
Save nikki93/65ea05c8eeb1172b649bbb0c9c057606 to your computer and use it in GitHub Desktop.
diff --git a/apps/gl-test/EXGLView.js b/apps/gl-test/EXGLView.js
index b2f9bd7..a12e422 100644
--- a/apps/gl-test/EXGLView.js
+++ b/apps/gl-test/EXGLView.js
@@ -64,11 +64,17 @@ const getGl = (exglContextId) => {
{ func: 'vertexAttrib2fv', arg: 1, type: Float32Array },
{ func: 'vertexAttrib3fv', arg: 1, type: Float32Array },
{ func: 'vertexAttrib4fv', arg: 1, type: Float32Array },
+ { func: 'bufferData', arg: 1, type: Int32Array },
+ { func: 'texImage2D', arg: 8, type: Int32Array },
].forEach(({ func, arg, type }) => {
const old = gl[func];
gl[func] = (...args) => {
// NOTE: Keep this fast
- args[arg] = new type(args[arg]);
+ // args[arg] = new type(args[arg]);
+ // return old.apply(gl, args);
+ const orig = args[arg];
+ const conv = orig.buffer ? orig : new type(orig);
+ args[arg] = new Int32Array(conv.buffer, conv.byteOffset);
return old.apply(gl, args);
};
});
diff --git a/exponent/cpp/EXGL.cpp b/exponent/cpp/EXGL.cpp
index d8c7d99..2e3b098 100644
--- a/exponent/cpp/EXGL.cpp
+++ b/exponent/cpp/EXGL.cpp
@@ -190,11 +190,12 @@ public:
// --- Init/destroy and JS object binding ------------------------------------
private:
JSObjectRef jsGl;
+ JSObjectRef jsTypedArrayAPI;
public:
EXGLContext(JSGlobalContextRef jsCtx) {
// Prepare context
- JSContextPrepareTypedArrayAPI(jsCtx);
+ jsTypedArrayAPI = JSContextPrepareTypedArrayAPI(jsCtx);
// Create JS version of us
auto jsClass = JSClassCreate(&kJSClassDefinitionEmpty);
@@ -235,9 +236,9 @@ private:
return std::shared_ptr<char>(EXJSValueToUTF8CStringMalloc(jsCtx, jsVal, NULL), free);
}
- static inline std::shared_ptr<void> jsValueToSharedArray(JSContextRef jsCtx, JSValueRef jsVal,
+ inline std::shared_ptr<void> jsValueToSharedArray(JSContextRef jsCtx, JSValueRef jsVal,
size_t *len) noexcept {
- return std::shared_ptr<void>(JSObjectGetTypedArrayDataMalloc(jsCtx, (JSObjectRef) jsVal, len), free);
+ return std::shared_ptr<void>(JSObjectGetTypedArrayDataMalloc(jsCtx, jsTypedArrayAPI, (JSObjectRef) jsVal, len), free);
}
static inline void *bufferOffset(GLint offset) noexcept {
diff --git a/exponent/cpp/EXJSConvertTypedArray.c b/exponent/cpp/EXJSConvertTypedArray.c
index fc1fdc7..353d77d 100644
--- a/exponent/cpp/EXJSConvertTypedArray.c
+++ b/exponent/cpp/EXJSConvertTypedArray.c
@@ -152,6 +152,9 @@ static JSObjectRef GetConstructor(JSContextRef ctx, JSTypedArrayType type) {
// Create a typed array view from another typed array or arraybuffer
static JSObjectRef GetView(JSContextRef ctx, JSObjectRef object, JSTypedArrayType type, size_t count) {
+ if (type == kJSTypedArrayTypeInt32Array) {
+ return object;
+ }
JSTypedArrayType currentType = JSObjectGetTypedArrayType(ctx, object);
if( currentType == kJSTypedArrayTypeNone ) {
return NULL;
@@ -259,7 +262,7 @@ static JSObjectRef CreateAppendDataCallbackState(JSContextRef ctx) {
-void JSContextPrepareTypedArrayAPI(JSContextRef ctx) {
+JSObjectRef JSContextPrepareTypedArrayAPI(JSContextRef ctx) {
// The __ejTypedArrayType property is read only, not enumerable
JSPropertyAttributes attributes =
// kJSPropertyAttributeReadOnly |
@@ -288,14 +291,20 @@ void JSContextPrepareTypedArrayAPI(JSContextRef ctx) {
JSObjectRef global = JSContextGetGlobalObject(ctx);
JSObjectSetProperty(ctx, global, jsInternalStateName, jsCallbackStateObject, attributes, NULL);
JSStringRelease(jsInternalStateName);
+
+ return jsCallbackStateObject;
}
JSTypedArrayType JSObjectGetTypedArrayType(JSContextRef ctx, JSObjectRef object) {
+ static JSStringRef propertyStr = NULL;
if (!JSValueIsObject(ctx, object)) {
return kJSTypedArrayTypeNone;
}
- JSValueRef jsType = GetPropertyNamed(ctx, object, "__ejTypedArrayType");
+ if (!propertyStr) {
+ propertyStr = JSStringCreateWithUTF8CString("__ejTypedArrayType");
+ }
+ JSValueRef jsType = JSObjectGetProperty(ctx, object, propertyStr, NULL);
if (!JSValueToBoolean(ctx, jsType)) {
return kJSTypedArrayTypeNone;
};
@@ -327,8 +336,12 @@ JSObjectRef JSObjectMakeTypedArrayWithData(JSContextRef ctx, JSTypedArrayType ar
}
-void *JSObjectGetTypedArrayDataMalloc(JSContextRef ctx, JSObjectRef object, size_t *plength) {
- size_t length = GetInt32(ctx, GetPropertyNamed(ctx, object, "byteLength"));
+void *JSObjectGetTypedArrayDataMalloc(JSContextRef ctx, JSObjectRef jsState, JSObjectRef object, size_t *plength) {
+ static JSStringRef byteLengthStr = NULL;
+ if (!byteLengthStr) {
+ byteLengthStr = JSStringCreateWithUTF8CString("byteLength");
+ }
+ size_t length = GetInt32(ctx, JSObjectGetProperty(ctx, object, byteLengthStr, NULL));
if (plength) {
*plength = length;
}
@@ -356,7 +369,7 @@ void *JSObjectGetTypedArrayDataMalloc(JSContextRef ctx, JSObjectRef object, size
return NULL;
}
- JSObjectRef jsState = (JSObjectRef)GetPropertyNamed(ctx, JSContextGetGlobalObject(ctx), "__ejTypedArrayState");
+ // JSObjectRef jsState = (JSObjectRef)GetPropertyNamed(ctx, JSContextGetGlobalObject(ctx), "__ejTypedArrayState");
AppendDataCallbackState *state = JSObjectGetPrivate(jsState);
state->currentDataPtr = data;
diff --git a/exponent/cpp/EXJSConvertTypedArray.h b/exponent/cpp/EXJSConvertTypedArray.h
index da35879..fc160b3 100644
--- a/exponent/cpp/EXJSConvertTypedArray.h
+++ b/exponent/cpp/EXJSConvertTypedArray.h
@@ -45,8 +45,9 @@ typedef enum {
@function
@abstract Setup the JSContext for use of the Typed Array functions.
@param ctx The execution context to use
+@result A cached object to pass into JSObjectGetTypedArrayDataMalloc(...)
*/
-void JSContextPrepareTypedArrayAPI(JSContextRef ctx);
+JSObjectRef JSContextPrepareTypedArrayAPI(JSContextRef ctx);
/*!
@function
@@ -82,12 +83,13 @@ JSObjectRef JSObjectMakeTypedArrayWithData(JSContextRef ctx, JSTypedArrayType ar
@function
@abstract Returns a copy of the Typed Array's data
@param ctx The execution context to use.
+@param jsState The return value of JSContextPRepareTypedArrayAPI(...) above
@param object The JSObject whose Typed Array data you want to obtain.
@param plength Place to put length of data, ignored if NULL
@result Pointer to start of a copy of the Typed Array's data or NULL if the JSObject is not a Typed Array.
The buffer must be free()'d later.
*/
-void *JSObjectGetTypedArrayDataMalloc(JSContextRef ctx, JSObjectRef object, size_t *plength);
+void *JSObjectGetTypedArrayDataMalloc(JSContextRef ctx, JSObjectRef jsState, JSObjectRef object, size_t *plength);
/*!
@function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment