JSBool ReflectHistogramSnapshot(JSContext *cx, JSObject *obj, Histogram *h) { Histogram::SampleSet ss; h->SnapshotSample(&ss); JSObject *counts_array; JSObject *rarray; jsval static_histogram = h->flags() && Histogram::kUmaTargetedHistogramFlag ? JSVAL_TRUE : JSVAL_FALSE; const size_t count = h->bucket_count(); if (!(JS_DefineProperty(cx, obj, "min", INT_TO_JSVAL(h->declared_min()), NULL, NULL, JSPROP_ENUMERATE) && JS_DefineProperty(cx, obj, "max", INT_TO_JSVAL(h->declared_max()), NULL, NULL, JSPROP_ENUMERATE) && JS_DefineProperty(cx, obj, "histogram_type", INT_TO_JSVAL(h->histogram_type()), NULL, NULL, JSPROP_ENUMERATE) && JS_DefineProperty(cx, obj, "sum", DOUBLE_TO_JSVAL(ss.sum()), NULL, NULL, JSPROP_ENUMERATE) && (rarray = JS_NewArrayObject(cx, count, NULL)) && JS_DefineProperty(cx, obj, "ranges", OBJECT_TO_JSVAL(rarray), NULL, NULL, JSPROP_ENUMERATE) && FillRanges(cx, rarray, h) && (counts_array = JS_NewArrayObject(cx, count, NULL)) && JS_DefineProperty(cx, obj, "counts", OBJECT_TO_JSVAL(counts_array), NULL, NULL, JSPROP_ENUMERATE) && JS_DefineProperty(cx, obj, "static", static_histogram, NULL, NULL, JSPROP_ENUMERATE) )) { return JS_FALSE; } for (size_t i = 0; i < count; i++) { if (!JS_DefineElement(cx, counts_array, i, INT_TO_JSVAL(ss.counts(i)), NULL, NULL, JSPROP_ENUMERATE)) { return JS_FALSE; } } return JS_TRUE; }
enum reflectStatus ReflectHistogramSnapshot(JSContext *cx, JSObject *obj, Histogram *h) { Histogram::SampleSet ss; h->SnapshotSample(&ss); // We don't want to reflect corrupt histograms. if (h->FindCorruption(ss) != Histogram::NO_INCONSISTENCIES) { return REFLECT_CORRUPT; } JSObject *counts_array; JSObject *rarray; const size_t count = h->bucket_count(); if (!(JS_DefineProperty(cx, obj, "min", INT_TO_JSVAL(h->declared_min()), NULL, NULL, JSPROP_ENUMERATE) && JS_DefineProperty(cx, obj, "max", INT_TO_JSVAL(h->declared_max()), NULL, NULL, JSPROP_ENUMERATE) && JS_DefineProperty(cx, obj, "histogram_type", INT_TO_JSVAL(h->histogram_type()), NULL, NULL, JSPROP_ENUMERATE) && JS_DefineProperty(cx, obj, "sum", DOUBLE_TO_JSVAL(ss.sum()), NULL, NULL, JSPROP_ENUMERATE) && (rarray = JS_NewArrayObject(cx, count, NULL)) && JS_DefineProperty(cx, obj, "ranges", OBJECT_TO_JSVAL(rarray), NULL, NULL, JSPROP_ENUMERATE) && FillRanges(cx, rarray, h) && (counts_array = JS_NewArrayObject(cx, count, NULL)) && JS_DefineProperty(cx, obj, "counts", OBJECT_TO_JSVAL(counts_array), NULL, NULL, JSPROP_ENUMERATE) )) { return REFLECT_FAILURE; } for (size_t i = 0; i < count; i++) { if (!JS_DefineElement(cx, counts_array, i, INT_TO_JSVAL(ss.counts(i)), NULL, NULL, JSPROP_ENUMERATE)) { return REFLECT_FAILURE; } } return REFLECT_OK; }
JSObject* make_array( JSContext* ctx, int size ) { // https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_NewArrayObject #if MOZJS_MAJOR_VERSION >= 31 return JS_NewArrayObject(ctx, size); #else return JS_NewArrayObject(ctx, size, NULL); #endif }
static void create_search_paths (GjsRequire *self) { JSAutoCompartment ac(self->priv->context, JS_GetGlobalObject (self->priv->context)); /* create exported (accessible from JS) 'paths' array */ self->priv->exported_paths = JS_NewArrayObject (self->priv->context, 0, NULL); g_assert (self->priv->exported_paths != NULL); /* create private paths array */ self->priv->private_paths = JS_NewArrayObject (self->priv->context, 0, NULL); g_assert (self->priv->private_paths != NULL); }
static jsval to_javascript_object(JSContext *context, PyObject *value) { if (PyString_Check(value)) { JSString *obj = JS_NewStringCopyN(context, PyString_AsString(value), PyString_Size(value)); return STRING_TO_JSVAL(obj); } else if (PyUnicode_Check(value)) { PyObject *encoded = PyUnicode_AsUTF8String(value); JSString *obj = JS_NewStringCopyN(context, PyString_AsString(encoded), PyString_Size(encoded)); Py_DECREF(encoded); return STRING_TO_JSVAL(obj); } else if (PyFloat_Check(value)) { return DOUBLE_TO_JSVAL(PyFloat_AsDouble(value)); } else if (PyInt_Check(value)) { return INT_TO_JSVAL(PyInt_AsLong(value)); } else if (PyLong_Check(value)) { return INT_TO_JSVAL(PyLong_AsLong(value)); } else if (PyList_Check(value)) { JSObject *obj = JS_NewArrayObject(context, 0, NULL); int i; for (i = 0; i < PyList_Size(value); i++) { jsval item = to_javascript_object(context, PyList_GetItem(value, i)); JS_SetElement(context, obj, i, &item); } return OBJECT_TO_JSVAL(obj); } else if (PyTuple_Check(value)) { JSObject *obj = JS_NewArrayObject(context, 0, NULL); int i; for (i = 0; i < PyTuple_Size(value); i++) { jsval item = to_javascript_object(context, PyTuple_GetItem(value, i)); JS_SetElement(context, obj, i, &item); } return OBJECT_TO_JSVAL(obj); } else if (PyDict_Check(value)) { JSObject *obj = JS_NewObject(context, NULL, NULL, NULL); populate_javascript_object(context, obj, value); return OBJECT_TO_JSVAL(obj); } else if (PyDateTime_Check(value)) { JSObject *obj = JS_NewDateObject(context, PyDateTime_GET_YEAR(value), PyDateTime_GET_MONTH(value) - 1, PyDateTime_GET_DAY(value), PyDateTime_DATE_GET_HOUR(value), PyDateTime_DATE_GET_MINUTE(value), PyDateTime_DATE_GET_SECOND(value)); return OBJECT_TO_JSVAL(obj); } else { return JSVAL_NULL; } }
nsresult mozilla::dom::bluetooth::StringArrayToJSArray(JSContext* aCx, JSObject* aGlobal, const nsTArray<nsString>& aSourceArray, JSObject** aResultArray) { NS_ASSERTION(aCx, "Null context!"); NS_ASSERTION(aGlobal, "Null global!"); JSAutoRequest ar(aCx); JSAutoEnterCompartment ac; if (!ac.enter(aCx, aGlobal)) { NS_WARNING("Failed to enter compartment!"); return NS_ERROR_FAILURE; } JSObject* arrayObj; if (aSourceArray.IsEmpty()) { arrayObj = JS_NewArrayObject(aCx, 0, nullptr); } else { uint32_t valLength = aSourceArray.Length(); mozilla::ScopedDeleteArray<jsval> valArray(new jsval[valLength]); JS::AutoArrayRooter tvr(aCx, valLength, valArray); for (PRUint32 index = 0; index < valLength; index++) { JSString* s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(), aSourceArray[index].Length()); if(!s) { NS_WARNING("Memory allocation error!"); return NS_ERROR_OUT_OF_MEMORY; } valArray[index] = STRING_TO_JSVAL(s); } arrayObj = JS_NewArrayObject(aCx, valLength, valArray); } if (!arrayObj) { return NS_ERROR_OUT_OF_MEMORY; } // XXX This is not what Jonas wants. He wants it to be live. // Followup at bug 717414 if (!JS_FreezeObject(aCx, arrayObj)) { return NS_ERROR_FAILURE; } *aResultArray = arrayObj; return NS_OK; }
nsresult ArchiveRequest::GetFilesResult(JSContext* aCx, JS::Value* aValue, nsTArray<nsCOMPtr<nsIDOMFile> >& aFileList) { JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, aFileList.Length(), nullptr)); if (!array) { return NS_ERROR_OUT_OF_MEMORY; } for (uint32_t i = 0; i < aFileList.Length(); ++i) { nsCOMPtr<nsIDOMFile> file = aFileList[i]; JS::Rooted<JS::Value> value(aCx); JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx)); nsresult rv = nsContentUtils::WrapNative(aCx, global, file, &NS_GET_IID(nsIDOMFile), value.address()); if (NS_FAILED(rv) || !JS_SetElement(aCx, array, i, &value)) { return NS_ERROR_FAILURE; } } aValue->setObject(*array); return NS_OK; }
static JSBool AfxGlobal_glGenTextures(JSContext *cx, unsigned argc, JS::Value *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); if(1 > args.length()) return JS_FALSE; uint32_t length; if(!JS::ToUint32(cx, args[0], &length)) return JS_FALSE; GLuint * textures = new GLuint[length]; jsval * jsTextures = new jsval[length]; glGenTextures(length, textures); for(uint32_t i=0; i<length; i++) { jsTextures[i] = JS_NumberValue(textures[i]); } delete textures; JSObject * retObj = JS_NewArrayObject(cx, length, jsTextures); delete jsTextures; if(!retObj) return JS_FALSE; args.rval().set(OBJECT_TO_JSVAL(retObj)); return JS_TRUE; }
static JSBool rpmaug_match(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { void * ptr = JS_GetInstancePrivate(cx, obj, &rpmaugClass, NULL); rpmaug aug = ptr; JSBool ok = JS_FALSE; const char * _path = NULL; char ** _matches = NULL; int nmatches; _METHOD_DEBUG_ENTRY(_debug); if (!(ok = JS_ConvertArguments(cx, argc, argv, "s", &_path))) goto exit; nmatches = rpmaugMatch(aug, _path, &_matches); if (nmatches <= 0) { /* not found */ *rval = JSVAL_VOID; goto exit; } else { JSObject *o; jsval v; int i; *rval = OBJECT_TO_JSVAL(o=JS_NewArrayObject(cx, 0, NULL)); for (i = 0; i < nmatches; i++) { v = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, _matches[i])); ok = JS_SetElement(cx, o, i, &v); _matches[i] = _free(_matches[i]); } _matches = _free(_matches); } ok = JS_TRUE; exit: return ok; }
// sequence<DOMString>? getSupportedExtensions(); bool JSB_glGetSupportedExtensions(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 0, cx, false, "Invalid number of arguments" ); JS::CallArgs args = JS::CallArgsFromVp(argc, vp); const GLubyte *extensions = glGetString(GL_EXTENSIONS); JS::RootedObject jsobj(cx, JS_NewArrayObject(cx, 0)); JSB_PRECONDITION2(jsobj, cx, false, "Error creating JS Object"); // copy, to be able to add '\0' size_t len = strlen((char*)extensions); GLubyte* copy = new (std::nothrow) GLubyte[len+1]; strncpy((char*)copy, (const char*)extensions, len ); int start_extension=0; int element=0; for( size_t i=0; i<len+1; i++) { if( copy[i]==' ' || copy[i]==',' || i==len ) { copy[i] = 0; JS::RootedValue str(cx, charptr_to_jsval(cx, (const char*)©[start_extension])); JS_SetElement(cx, jsobj, element++, str ); start_extension = i+1; i++; } } args.rval().set(OBJECT_TO_JSVAL(jsobj)); CC_SAFE_DELETE_ARRAY(copy); return true; }
bool TestTransferObject() { JS::RootedObject obj1(cx, CreateNewObject(8, 12)); CHECK(obj1); JS::RootedValue v1(cx, JS::ObjectValue(*obj1)); // Create an Array of transferable values. JS::AutoValueVector argv(cx); if (!argv.append(v1)) return false; JS::RootedObject obj(cx, JS_NewArrayObject(cx, JS::HandleValueArray::subarray(argv, 0, 1))); CHECK(obj); JS::RootedValue transferable(cx, JS::ObjectValue(*obj)); JSAutoStructuredCloneBuffer cloned_buffer(JS::StructuredCloneScope::SameProcessSameThread, nullptr, nullptr); CHECK(cloned_buffer.write(cx, v1, transferable, nullptr, nullptr)); JS::RootedValue v2(cx); CHECK(cloned_buffer.read(cx, &v2, nullptr, nullptr)); JS::RootedObject obj2(cx, v2.toObjectOrNull()); CHECK(VerifyObject(obj2, 8, 12, true)); CHECK(JS_IsDetachedArrayBufferObject(obj1)); return true; }
nsresult KeyPath::ExtractKeyAsJSVal(JSContext* aCx, const JS::Value& aValue, JS::Value* aOutVal) const { NS_ASSERTION(IsValid(), "This doesn't make sense!"); if (IsString()) { return GetJSValFromKeyPathString(aCx, aValue, mStrings[0], aOutVal, DoNotCreateProperties, nullptr, nullptr); } const uint32_t len = mStrings.Length(); JS::Rooted<JSObject*> arrayObj(aCx, JS_NewArrayObject(aCx, len)); if (!arrayObj) { return NS_ERROR_OUT_OF_MEMORY; } JS::Rooted<JS::Value> value(aCx); for (uint32_t i = 0; i < len; ++i) { nsresult rv = GetJSValFromKeyPathString(aCx, aValue, mStrings[i], value.address(), DoNotCreateProperties, nullptr, nullptr); if (NS_FAILED(rv)) { return rv; } if (!JS_DefineElement(aCx, arrayObj, i, value, JSPROP_ENUMERATE)) { IDB_REPORT_INTERNAL_ERR(); return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } } aOutVal->setObject(*arrayObj); return NS_OK; }
// sequence<WebGLShader>? getAttachedShaders(WebGLProgram? program); JSBool JSB_glGetAttachedShaders(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 1, cx, JS_FALSE, "Invalid number of arguments" ); jsval *argvp = JS_ARGV(cx,vp); JSBool ok = JS_TRUE; uint32_t arg0; ok &= jsval_to_uint( cx, *argvp++, &arg0 ); JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments"); GLsizei length; glGetProgramiv(arg0, GL_ATTACHED_SHADERS, &length); GLuint* buffer = new GLuint[length]; memset(buffer, 0, length * sizeof(GLuint)); //Fix bug 2448, it seems that glGetAttachedShaders will crash if we send NULL to the third parameter (eg Windows), same as in lua binding GLsizei realShaderCount = 0; glGetAttachedShaders(arg0, length, &realShaderCount, buffer); JSObject *jsobj = JS_NewArrayObject(cx, length, NULL); JSB_PRECONDITION2(jsobj, cx, JS_FALSE, "Error creating JS Object"); for( int i=0; i<length; i++) { jsval e = INT_TO_JSVAL(buffer[i]); JS_SetElement(cx, jsobj, i, &e ); } JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsobj)); CC_SAFE_DELETE_ARRAY(buffer); return JS_TRUE; }
static JSBool SMJS_FUNCTION(widget_get_interfaces) { u32 i, count; char *ifce_name; JSObject *list; jsuint idx; jsval v; SMJS_OBJ SMJS_ARGS GF_WidgetInstance *wid = (GF_WidgetInstance *)SMJS_GET_PRIVATE(c, obj); if (!wid) return JS_FALSE; if (!argc || !JSVAL_IS_STRING(argv[0])) return JS_TRUE; ifce_name = SMJS_CHARS(c, argv[0]); list = JS_NewArrayObject(c, 0, 0); count = gf_list_count(wid->bound_ifces); for (i=0; i<count; i++) { GF_WidgetInterfaceInstance *ifce = gf_list_get(wid->bound_ifces, i); if (strcmp(ifce->ifce->type, ifce_name)) continue; widget_interface_js_bind(c, ifce); JS_GetArrayLength(c, list, &idx); v = OBJECT_TO_JSVAL(ifce->obj); JS_SetElement(c, list, idx, &v); } SMJS_SET_RVAL( OBJECT_TO_JSVAL(list) ); SMJS_FREE(c, ifce_name); return JS_TRUE; }
JSBool js_cocos2dx_studio_ColliderBody_getCalculatedVertexList(JSContext *cx, uint32_t argc, jsval *vp) { JSObject *obj = JS_THIS_OBJECT(cx, vp); js_proxy_t *proxy = jsb_get_js_proxy(obj); cocostudio::ColliderBody* cobj = (cocostudio::ColliderBody *)(proxy ? proxy->ptr : nullptr); JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object"); if (argc == 0) { const std::vector<cocos2d::Point>& ret = cobj->getCalculatedVertexList(); JSObject *jsretArr = JS_NewArrayObject(cx, 0, nullptr); jsval jsret; //CCObject* obj; int i = 0; for(std::vector<cocos2d::Point>::const_iterator it = ret.begin(); it != ret.end(); it++) { const cocos2d::Point& point = *it; JSObject *tmp = JS_NewObject(cx, NULL, NULL, NULL); if (!tmp) break; JSBool ok = JS_DefineProperty(cx, tmp, "x", DOUBLE_TO_JSVAL(point.x), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) && JS_DefineProperty(cx, tmp, "y", DOUBLE_TO_JSVAL(point.y), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT); jsval jsTmp = OBJECT_TO_JSVAL(tmp); if(!ok || !JS_SetElement(cx, jsretArr, i, &jsTmp)) { break; } ++i; } jsret = OBJECT_TO_JSVAL(jsretArr); JS_SET_RVAL(cx, vp, jsret); return JS_TRUE; } JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0); return JS_FALSE; }
// Return file contents as an array of lines. Assume file is UTF-8 encoded text. // // lines = readFileLines(filename); // filename: VFS filename (may include path) CScriptVal JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, std::wstring filename) { // // read file // CVFSFile file; if (file.Load(g_VFS, filename) != PSRETURN_OK) return JSVAL_NULL; CStr contents = file.DecodeUTF8(); // assume it's UTF-8 // Fix CRLF line endings. (This function will only ever be used on text files.) contents.Replace("\r\n", "\n"); // // split into array of strings (one per line) // std::stringstream ss(contents); JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); JSObject* line_array = JS_NewArrayObject(cx, 0, NULL); std::string line; int cur_line = 0; while (std::getline(ss, line)) { // Decode each line as UTF-8 JS::RootedValue val(cx); ScriptInterface::ToJSVal(cx, val.get(), CStr(line).FromUTF8()); JS_SetElement(cx, line_array, cur_line++, val.address()); } return OBJECT_TO_JSVAL( line_array ); }
JSBool JsIndexer::readMetadataImages(JSContext *cx,JSObject *obj,uintN argc,jsval *argv,jsval *rval) { char *path = {0}; if ( !JS_ConvertArguments(cx,argc,argv,"s",&path) ) { return Engine::throwUsageError(cx,argv); } std::string utf8Path = path; // the given path should be formatted as utf-8 // convert utf-8 path to utf-16 since the indexer uses utf-16 internally std::wstring utf16Path; utf8::utf8to16(utf8Path.begin(),utf8Path.end(),back_inserter(utf16Path)); JSObject *arr = JS_NewArrayObject(cx,0,NULL); if ( arr!=NULL ) { int count = 0; std::list<MetadataImage::Ptr> images; Indexer::getInstance()->readMetadata(utf16Path,NULL,&images); for ( std::list<MetadataImage::Ptr>::iterator iter=images.begin(); iter!=images.end(); iter++ ) { jsval element = OBJECT_TO_JSVAL(JsMetadataImage::jsInstance(cx,obj,*iter)); if ( JS_SetElement(cx,arr,count,&element)==JS_TRUE ) { count++; } } *rval = OBJECT_TO_JSVAL(arr); } return JS_TRUE; }
nsresult KeyPath::ExtractKeyAsJSVal(JSContext* aCx, const JS::Value& aValue, JS::Value* aOutVal) const { NS_ASSERTION(IsValid(), "This doesn't make sense!"); if (IsString()) { return GetJSValFromKeyPathString(aCx, aValue, mStrings[0], aOutVal, DoNotCreateProperties, nullptr, nullptr); } const uint32_t len = mStrings.Length(); js::RootedObject arrayObj(aCx, JS_NewArrayObject(aCx, len, nullptr)); if (!arrayObj) { return NS_ERROR_OUT_OF_MEMORY; } JS::Value value; for (uint32_t i = 0; i < len; ++i) { nsresult rv = GetJSValFromKeyPathString(aCx, aValue, mStrings[i], &value, DoNotCreateProperties, nullptr, nullptr); if (NS_FAILED(rv)) { return rv; } if (!JS_SetElement(aCx, arrayObj, i, &value)) { return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } } *aOutVal = OBJECT_TO_JSVAL(arrayObj); return NS_OK; }
NS_IMETHODIMP IDBIndex::GetKeyPath(JSContext* aCx, jsval* aVal) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); if (UsesKeyPathArray()) { JSObject* array = JS_NewArrayObject(aCx, mKeyPathArray.Length(), nsnull); if (!array) { NS_WARNING("Failed to make array!"); return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } for (PRUint32 i = 0; i < mKeyPathArray.Length(); ++i) { jsval val; nsString tmp(mKeyPathArray[i]); if (!xpc::StringToJsval(aCx, tmp, &val)) { return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } if (!JS_SetElement(aCx, array, i, &val)) { return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } } *aVal = OBJECT_TO_JSVAL(array); } else { nsString tmp(mKeyPath); if (!xpc::StringToJsval(aCx, tmp, aVal)) { return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } } return NS_OK; }
jsval to_js_empty_array(JSContext* cx) { JSObject* ret = JS_NewArrayObject(cx, 0, NULL); if(ret == NULL) return JSVAL_VOID; return OBJECT_TO_JSVAL(ret); }
// sequence<DOMString>? getSupportedExtensions(); JSBool JSB_glGetSupportedExtensions(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 0, cx, JS_FALSE, "Invalid number of arguments" ); const GLubyte *extensions = glGetString(GL_EXTENSIONS); JSObject *jsobj = JS_NewArrayObject(cx, 0, NULL); JSB_PRECONDITION2(jsobj, cx, JS_FALSE, "Error creating JS Object"); // copy, to be able to add '\0' size_t len = strlen((char*)extensions); GLubyte* copy = new GLubyte[len+1]; strncpy((char*)copy, (const char*)extensions, len ); int start_extension=0; int element=0; for( int i=0; i<len+1; i++) { if( copy[i]==' ' || copy[i]==',' || i==len ) { copy[i] = 0; jsval str = charptr_to_jsval(cx, (const char*)©[start_extension]); JS_SetElement(cx, jsobj, element++, &str ); start_extension = i+1; i++; } } JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsobj)); CC_SAFE_DELETE_ARRAY(copy); return JS_TRUE; }
NS_IMETHODIMP nsScriptableRegion::GetRects(JSContext* aCx, JS::MutableHandle<JS::Value> aRects) { uint32_t numRects = mRegion.GetNumRects(); if (!numRects) { aRects.setNull(); return NS_OK; } JS::Rooted<JSObject*> destArray(aCx, JS_NewArrayObject(aCx, numRects * 4)); if (!destArray) { return NS_ERROR_OUT_OF_MEMORY; } aRects.setObject(*destArray); uint32_t n = 0; nsIntRegionRectIterator iter(mRegion); const nsIntRect *rect; while ((rect = iter.Next())) { if (!JS_DefineElement(aCx, destArray, n, rect->x, JSPROP_ENUMERATE) || !JS_DefineElement(aCx, destArray, n + 1, rect->y, JSPROP_ENUMERATE) || !JS_DefineElement(aCx, destArray, n + 2, rect->width, JSPROP_ENUMERATE) || !JS_DefineElement(aCx, destArray, n + 3, rect->height, JSPROP_ENUMERATE)) { return NS_ERROR_FAILURE; } n += 4; } return NS_OK; }
static JSBool js_conio_gettext(JSContext *cx, uintN argc, jsval *arglist) { jsval *argv=JS_ARGV(cx, arglist); int32 args[4]; unsigned char *result; int i; int size; JSObject *array; jsval val; jsrefcount rc; JS_SET_RVAL(cx, arglist, JSVAL_VOID); /* default values: */ args[0]=1; args[1]=1; args[2]=cio_textinfo.screenwidth; args[3]=cio_textinfo.screenheight; if(argc > 4) return(JS_FALSE); for(i=0; i<(int)argc; i++) { if(!JSVAL_IS_NUMBER(argv[i])) return(JS_FALSE); if(!JS_ValueToInt32(cx, argv[i], &args[i])) return(JS_FALSE); } if(args[0] < 1 || args[1] < 1 || args[2] < 1 || args[3] < 1 || args[0] > args[2] || args[1] > args[3] || args[2] > cio_textinfo.screenwidth || args[3] > cio_textinfo.screenheight) { JS_SET_RVAL(cx, arglist,JSVAL_FALSE); return(JS_TRUE); } size=(args[2]-args[0]+1)*(args[3]-args[1]+1)*2; result=(unsigned char *)malloc(size); if(result==NULL) return(JS_FALSE); rc=JS_SUSPENDREQUEST(cx); if(gettext(args[0], args[1], args[2], args[3], result)) { JS_RESUMEREQUEST(cx, rc); array=JS_NewArrayObject(cx, 0, NULL); for(i=0; i<size; i++) { val=UINT_TO_JSVAL(result[i]); if(!JS_SetElement(cx, array, i, &val)) { free(result); return(JS_FALSE); } } JS_SET_RVAL(cx, arglist,OBJECT_TO_JSVAL(array)); } else { JS_RESUMEREQUEST(cx, rc); JS_SET_RVAL(cx, arglist,JSVAL_NULL); } free(result); return(JS_TRUE); }
static JSBool Node_set_children (JSContext *cxt, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { int i; jsval val; gezira_node *node = (gezira_node *) JS_GetPrivate (cxt, obj); JSObject *array; if (argc && JS_IsArrayObject (cxt, JSVAL_TO_OBJECT (argv[0]))) array = JSVAL_TO_OBJECT (argv[0]); else array = JS_NewArrayObject (cxt, argc, argv); JS_GetArrayLength (cxt, array, &node->n); free (node->elements); node->elements = (gezira_stream **) malloc (node->n * sizeof (gezira_stream *)); for (i = 0; i < node->n; i++) { JS_GetElement (cxt, array, i, &val); node->elements[i] = (gezira_stream *) JS_GetPrivate (cxt, JSVAL_TO_OBJECT (val)); } /* Need references to children to keep GC happy */ val = OBJECT_TO_JSVAL (array); JS_SetProperty (cxt, obj, "_children", &val); *rval = OBJECT_TO_JSVAL (obj); return JS_TRUE; }
// sequence<WebGLShader>? getAttachedShaders(WebGLProgram? program); bool JSB_glGetAttachedShaders(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 1, cx, false, "Invalid number of arguments" ); JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; uint32_t arg0; ok &= jsval_to_uint( cx, args.get(0), &arg0 ); JSB_PRECONDITION2(ok, cx, false, "Error processing arguments"); GLsizei length; glGetProgramiv(arg0, GL_ATTACHED_SHADERS, &length); GLuint* buffer = new (std::nothrow) GLuint[length]; memset(buffer, 0, length * sizeof(GLuint)); //Fix bug 2448, it seems that glGetAttachedShaders will crash if we send NULL to the third parameter (eg Windows), same as in lua binding GLsizei realShaderCount = 0; glGetAttachedShaders(arg0, length, &realShaderCount, buffer); JS::RootedObject jsobj(cx, JS_NewArrayObject(cx, length)); JSB_PRECONDITION2(jsobj, cx, false, "Error creating JS Object"); for( int i=0; i<length; i++) { JS::RootedValue e(cx, INT_TO_JSVAL(buffer[i])); JS_SetElement(cx, jsobj, i, e ); } args.rval().set(OBJECT_TO_JSVAL(jsobj)); CC_SAFE_DELETE_ARRAY(buffer); return true; }
static JSObject * rpmtsLoadNVRA(JSContext *cx, JSObject *obj) { void * ptr = JS_GetInstancePrivate(cx, obj, &rpmtsClass, NULL); rpmts ts = ptr; JSObject * NVRA = JS_NewArrayObject(cx, 0, NULL); ARGV_t keys = NULL; int nkeys; int xx; int i; if (ts->rdb == NULL) (void) rpmtsOpenDB(ts, O_RDONLY); xx = rpmdbMireApply(rpmtsGetRdb(ts), RPMTAG_NVRA, RPMMIRE_STRCMP, NULL, &keys); nkeys = argvCount(keys); if (keys) for (i = 0; i < nkeys; i++) { JSString * valstr = JS_NewStringCopyZ(cx, keys[i]); jsval id = STRING_TO_JSVAL(valstr); JS_SetElement(cx, NVRA, i, &id); } JS_DefineProperty(cx, obj, "NVRA", OBJECT_TO_JSVAL(NVRA), NULL, NULL, JSPROP_ENUMERATE); if (_debug) fprintf(stderr, "==> %s(%p,%p) ptr %p NVRA %p\n", __FUNCTION__, cx, obj, ptr, NVRA); return NVRA; }
JSBool JsSite::getAttributeNames(JSContext *cx,JSObject *obj,uintN argc,jsval *argv,jsval *rval) { Site *site = (Site*)JS_GetPrivate(cx,obj); JSObject *arr = JS_NewArrayObject(cx,0,NULL); if ( arr!=NULL ) { int count=0; std::map<std::string,std::string> attributes = site->getAttributes(); std::map<std::string,std::string>::const_iterator iter; for ( iter=attributes.begin(); iter!=attributes.end(); iter++ ) { JSString *str = JS_NewStringCopyN(cx,iter->first.c_str(),iter->first.length()); jsval element = STRING_TO_JSVAL(str); if ( JS_SetElement(cx,arr,count,&element)==JS_TRUE ) { count++; } } *rval = OBJECT_TO_JSVAL(arr); } return JS_TRUE; }
JSBool webMonsterHash(JSContext *cx, uintN argc, jsval *vp) { char *data = NULL; if(JSVAL_IS_STRING(*JS_ARGV(cx,vp))){ data = JS_EncodeString(cx,JSVAL_TO_STRING(*JS_ARGV(cx,vp))); if(data){ encoding rtn; if(!encode(&rtn,data)){ JSObject *array = NULL; array = JS_NewArrayObject(cx, 3, NULL); if(array){ jsval string; string = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, rtn.key)); JS_SetElement(cx,array, 0,&string); string = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, rtn.sanity)); JS_SetElement(cx,array, 0,&string); string = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, rtn.data)); JS_SetElement(cx,array, 0,&string); JS_SET_RVAL(cx,vp,OBJECT_TO_JSVAL(array)); } else { JS_SET_RVAL(cx,vp,JSVAL_NULL); } //and free all the object resources: if(rtn.key)free(rtn.key); if(rtn.data)free(rtn.data); if(rtn.sanity)free(rtn.sanity); } JS_free(cx,data); } } return JS_TRUE; }
JSObject* xgg_new_nodelist(XJSECTX* pctxa, JSObject* poparent) { JSBool b1; #ifdef NODELISTISCLASS JSObject* pobjbase = 0; JSObject* pobj = JS_DefineObject(pctxa->pctx, poparent, pszobjname, &class_nodelist, pobjbase, JSPROP_PERMANENT | JSPROP_READONLY | JSPROP_ENUMERATE); #else //NODELISTISCLASS JSObject* pobj = JS_NewArrayObject(pctxa->pctx, 0, NULL); if(pobj == 0) { XJSE_TRACE("(E) JS_NewArrayObject() failed!"); goto failed; } #if 0 b1 = JS_DefineFunctions(pctxa->pctx, pobj, funcs_nodelist); if(b1 != JS_TRUE) goto failed; b1 = JS_DefineProperties(pctxa->pctx, pobj, props_nodelist); if(b1 != JS_TRUE) goto failed; #endif #endif //NODELISTISCLASS // b1 = xgg_nodelist_ctor(pctxa->pctx, pobj, 0, 0, 0); //args, rval if(b1 != JS_TRUE) { XJSE_TRACE("(E) xgg_nodelist_ctor failed!"); } // return pobj; failed: return 0; }
BuildDirEntListState(JSContext* cx_) : cx(cx_) { filename_array = JS_NewArrayObject(cx, 0, NULL); JS_AddObjectRoot(cx, &filename_array); cur_idx = 0; }