JSBool js_cocos2dx_extension_WebSocket_send(JSContext *cx, uint32_t argc, jsval *vp) { jsval *argv = JS_ARGV(cx, vp); JSObject *obj = JS_THIS_OBJECT(cx, vp); js_proxy_t *proxy = jsb_get_js_proxy(obj); WebSocket* cobj = (WebSocket *)(proxy ? proxy->ptr : NULL); JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object"); if(argc == 1){ do { if (JSVAL_IS_STRING(argv[0])) { std::string data; jsval_to_std_string(cx, argv[0], &data); cobj->send(data); break; } if (argv[0].isObject()) { uint8_t *bufdata = NULL; uint32_t len = 0; JSObject* jsobj = JSVAL_TO_OBJECT(argv[0]); if (JS_IsArrayBufferObject(jsobj)) { bufdata = JS_GetArrayBufferData(jsobj); len = JS_GetArrayBufferByteLength(jsobj); } else if (JS_IsArrayBufferViewObject(jsobj)) { bufdata = (uint8_t*)JS_GetArrayBufferViewData(jsobj); len = JS_GetArrayBufferViewByteLength(jsobj); } if (bufdata && len > 0) { cobj->send(bufdata, len); break; } } JS_ReportError(cx, "data type to be sent is unsupported."); } while (0); JS_SET_RVAL(cx, vp, JSVAL_VOID); return JS_TRUE; } JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0); return JS_TRUE; }
JSBool JSB_get_arraybufferview_dataptr( JSContext *cx, jsval vp, GLsizei *count, GLvoid **data ) { JSObject *jsobj; JSBool ok = JS_ValueToObject( cx, vp, &jsobj ); JSB_PRECONDITION2( ok && jsobj, cx, JS_FALSE, "Error converting value to object"); JSB_PRECONDITION2( JS_IsArrayBufferViewObject(jsobj), cx, JS_FALSE, "Not an ArrayBufferView object"); *data = JS_GetArrayBufferViewData(jsobj); *count = JS_GetArrayBufferViewByteLength(jsobj); return JS_TRUE; }
// convert buffer to json bool js_FlatUtil_flat2js(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; if (4 == argc) { std::string arg0; ok &= jsval_to_std_string(cx, args.get(0), &arg0); JSB_PRECONDITION2(ok, cx, false, "js_FlatUtil_flat2js : Error processing arguments"); cocos2d::Data arg1; uint8_t *bufdata = NULL; uint32_t len = 0; JSObject* jsobj = args.get(1).toObjectOrNull(); if (JS_IsArrayBufferObject(jsobj)) { bufdata = JS_GetArrayBufferData(jsobj); len = JS_GetArrayBufferByteLength(jsobj); } else if (JS_IsArrayBufferViewObject(jsobj)) { bufdata = (uint8_t*)JS_GetArrayBufferViewData(jsobj); len = JS_GetArrayBufferViewByteLength(jsobj); } JSB_PRECONDITION2(bufdata && len > 0, cx, false, "js_FlatUtil_flat2js : Error processing arguments"); arg1.copy(bufdata, len); std::string arg2; ok &= jsval_to_std_string(cx, args.get(2), &arg2); JSB_PRECONDITION2(ok, cx, false, "js_FlatUtil_flat2js : Error processing arguments"); bool arg3; arg3 = JS::ToBoolean(args.get(3)); auto ret = FlatUtil::flat2js(arg0, arg1, arg2, arg3); jsval jsret = JSVAL_NULL; jsret = std_string_to_jsval(cx, ret); args.rval().set(jsret); return true; } JS_ReportError(cx, "js_FlatUtil_flat2js : wrong number of arguments"); return false; }
bool TestViewType(JSContext *cx) { JS::Rooted<JSObject*> obj(cx, CreateViewType(cx)); CHECK(obj); CHECK(JS_IsArrayBufferViewObject(obj)); CHECK(JS_GetArrayBufferViewType(obj) == ExpectedType); CHECK(JS_GetArrayBufferViewByteLength(obj) == ExpectedByteLength); { JS::AutoCheckCannotGC nogc; T *data1 = static_cast<T*>(JS_GetArrayBufferViewData(obj, nogc)); T *data2; uint32_t len; CHECK(obj == GetObjectAs(obj, &len, &data2)); CHECK(data1 == data2); CHECK(len == ExpectedLength); } return true; }