JS_XDRValue(JSXDRState *xdr, jsval *vp) { uint32 type; if (xdr->mode == JSXDR_ENCODE) { if (JSVAL_IS_NULL(*vp)) type = JSVAL_XDRNULL; else if (JSVAL_IS_VOID(*vp)) type = JSVAL_XDRVOID; else type = JSVAL_TAG(*vp); } return JS_XDRUint32(xdr, &type) && XDRValueBody(xdr, type, vp); }
static JSBool navigator_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { FILE *fOut; JSString *str; size_t n, i; jschar *s; if (JSVAL_IS_INT(id)) { switch (JSVAL_TO_INT(id)) { case navigator_appversion: fOut = fopen(SETTERLOG, "a"); if (fOut == NULL) return JS_FALSE; if (JSVAL_IS_INT(*vp)) { fprintf(fOut, "navigator set property read-only %s = %d\n", navigator_property_str[JSVAL_TO_INT(id)], JSVAL_TO_INT(*vp)); } else if (JSVAL_IS_STRING(*vp)) { str = js_ValueToString(cx, *vp); if (!str) return JS_FALSE; if (JSSTRING_IS_DEPENDENT(str)) { n = JSSTRDEP_LENGTH(str); s = JSSTRDEP_CHARS(str); } else { n = str->length; s = str->chars; } fprintf(fOut, "navigator set property read-only %s = \"", navigator_property_str[JSVAL_TO_INT(id)]); if (n != 0) { for (i = 0; i < n; i++) fputc(s[i], fOut); } fprintf(fOut, "\"\n"); } else { fprintf(fOut, "navigator set property read-only %s to value of unknown type %x\n", navigator_property_str[JSVAL_TO_INT(id)], JSVAL_TAG(*vp)); } fclose (fOut); break; } } return JS_TRUE; }
/* --- helpers */ static JSObject * rpmhdrLoadTag(JSContext *cx, JSObject *obj, Header h, rpmTag tag, jsval *vp) { HE_t he = memset(alloca(sizeof(*he)), 0, sizeof(*he)); JSObject * arr; jsval v; JSObject * retobj = NULL; JSBool ok; const char * name = tagName(tag); int i; if (_debug) fprintf(stderr, "==> %s(%p,%p,%p,%s)\n", __FUNCTION__, cx, obj, h, name); he->tag = tag; if (headerGet(h, he, 0)) { if (_debug < 0) fprintf(stderr, "\t%s(%u) %u %p[%u]\n", name, (unsigned)he->tag, (unsigned)he->t, he->p.ptr, (unsigned)he->c); switch (he->t) { default: goto exit; /*@notreached@*/ break; case RPM_BIN_TYPE: /* XXX return as array of octets for now. */ case RPM_UINT8_TYPE: arr = JS_NewArrayObject(cx, 0, NULL); ok = JS_AddRoot(cx, &arr); for (i = 0; i < (int)he->c; i++) { v = INT_TO_JSVAL(he->p.ui8p[i]); ok = JS_SetElement(cx, arr, i, &v); } ok = JS_DefineProperty(cx, obj, name, (v=OBJECT_TO_JSVAL(arr)), NULL, NULL, JSPROP_ENUMERATE); (void) JS_RemoveRoot(cx, &arr); if (!ok) goto exit; retobj = obj; if (vp) *vp = v; break; case RPM_UINT16_TYPE: arr = JS_NewArrayObject(cx, 0, NULL); ok = JS_AddRoot(cx, &arr); for (i = 0; i < (int)he->c; i++) { v = INT_TO_JSVAL(he->p.ui16p[i]); ok = JS_SetElement(cx, arr, i, &v); } ok = JS_DefineProperty(cx, obj, name, (v=OBJECT_TO_JSVAL(arr)), NULL, NULL, JSPROP_ENUMERATE); (void) JS_RemoveRoot(cx, &arr); if (!ok) goto exit; retobj = obj; if (vp) *vp = v; break; case RPM_UINT32_TYPE: arr = JS_NewArrayObject(cx, 0, NULL); ok = JS_AddRoot(cx, &arr); for (i = 0; i < (int)he->c; i++) { if (!JS_NewNumberValue(cx, he->p.ui32p[i], &v)) v = JSVAL_VOID; ok = JS_SetElement(cx, arr, i, &v); } ok = JS_DefineProperty(cx, obj, name, (v=OBJECT_TO_JSVAL(arr)), NULL, NULL, JSPROP_ENUMERATE); (void) JS_RemoveRoot(cx, &arr); if (!ok) goto exit; retobj = obj; if (vp) *vp = v; break; case RPM_UINT64_TYPE: arr = JS_NewArrayObject(cx, 0, NULL); ok = JS_AddRoot(cx, &arr); for (i = 0; i < (int)he->c; i++) { if (!JS_NewNumberValue(cx, he->p.ui64p[i], &v)) v = JSVAL_VOID; ok = JS_SetElement(cx, arr, i, &v); } ok = JS_DefineProperty(cx, obj, name, (v=OBJECT_TO_JSVAL(arr)), NULL, NULL, JSPROP_ENUMERATE); (void) JS_RemoveRoot(cx, &arr); if (!ok) goto exit; retobj = obj; if (vp) *vp = v; break; case RPM_STRING_ARRAY_TYPE: arr = JS_NewArrayObject(cx, 0, NULL); ok = JS_AddRoot(cx, &arr); for (i = 0; i < (int)he->c; i++) { v = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, he->p.argv[i])); ok = JS_SetElement(cx, arr, i, &v); } ok = JS_DefineProperty(cx, obj, name, (v=OBJECT_TO_JSVAL(arr)), NULL, NULL, JSPROP_ENUMERATE); (void) JS_RemoveRoot(cx, &arr); if (!ok) goto exit; retobj = obj; if (vp) *vp = v; break; case RPM_I18NSTRING_TYPE: /* XXX FIXME: is this ever seen? */ fprintf(stderr, "==> FIXME: %s(%d) t %d %p[%u]\n", tagName(he->tag), he->tag, he->t, he->p.ptr, he->c); /*@fallthrough@*/ case RPM_STRING_TYPE: ok = JS_DefineProperty(cx, obj, name, (v=STRING_TO_JSVAL(JS_NewStringCopyZ(cx, he->p.str))), NULL, NULL, JSPROP_ENUMERATE); if (!ok) goto exit; retobj = obj; if (vp) *vp = v; break; } } exit: if (_debug < 0) fprintf(stderr, "\tretobj %p vp %p *vp 0x%lx(%u)\n", retobj, vp, (unsigned long)(vp ? *vp : 0), (unsigned)(vp ? JSVAL_TAG(*vp) : 0)); return retobj; }
static JSBool syck_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp) { if (_debug) fprintf(stderr, "==> %s(%p,%p,0x%lx[%u],0x%x,%p) property %s flags 0x%x{%s,%s,%s,%s,%s}\n", __FUNCTION__, cx, obj, (unsigned long)id, (unsigned)JSVAL_TAG(id), (unsigned)flags, objp, JS_GetStringBytes(JS_ValueToString(cx, id)), flags, (flags & JSRESOLVE_QUALIFIED) ? "qualified" : "", (flags & JSRESOLVE_ASSIGNING) ? "assigning" : "", (flags & JSRESOLVE_DETECTING) ? "detecting" : "", (flags & JSRESOLVE_DECLARING) ? "declaring" : "", (flags & JSRESOLVE_CLASSNAME) ? "classname" : ""); return JS_TRUE; }
JS_XDRValue(JSXDRState *xdr, jsval *vp) { uint32 type; if (xdr->mode == JSXDR_ENCODE) { if (JSVAL_IS_NULL(*vp)) type = JSVAL_XDRNULL; else if (JSVAL_IS_VOID(*vp)) type = JSVAL_XDRVOID; else type = JSVAL_TAG(*vp); } if (!JS_XDRUint32(xdr, &type)) return JS_FALSE; switch (type) { case JSVAL_XDRNULL: *vp = JSVAL_NULL; break; case JSVAL_XDRVOID: *vp = JSVAL_VOID; break; case JSVAL_STRING: { JSString *str; if (xdr->mode == JSXDR_ENCODE) str = JSVAL_TO_STRING(*vp); if (!JS_XDRString(xdr, &str)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) *vp = STRING_TO_JSVAL(str); break; } case JSVAL_DOUBLE: { jsdouble *dp; if (xdr->mode == JSXDR_ENCODE) dp = JSVAL_TO_DOUBLE(*vp); if (!JS_XDRDouble(xdr, &dp)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) *vp = DOUBLE_TO_JSVAL(dp); break; } case JSVAL_OBJECT: { JSObject *obj; if (xdr->mode == JSXDR_ENCODE) obj = JSVAL_TO_OBJECT(*vp); if (!js_XDRObject(xdr, &obj)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) *vp = OBJECT_TO_JSVAL(obj); break; } case JSVAL_BOOLEAN: { uint32 b; if (xdr->mode == JSXDR_ENCODE) b = (uint32) JSVAL_TO_BOOLEAN(*vp); if (!JS_XDRUint32(xdr, &b)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) *vp = BOOLEAN_TO_JSVAL((JSBool) b); break; } default: { uint32 i; JS_ASSERT(type & JSVAL_INT); if (xdr->mode == JSXDR_ENCODE) i = (uint32) JSVAL_TO_INT(*vp); if (!JS_XDRUint32(xdr, &i)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) *vp = INT_TO_JSVAL((int32) i); break; } } return JS_TRUE; }
void js_debug_argument(JSContext *cx, jsval vp) { func(" arg mem address %p", &vp); int tag = JSVAL_TAG(vp); func(" type tag is %i: %s",tag, (tag==0x0)?"object": (tag==0x1)?"integer": (tag==0x2)?"double": (tag==0x4)?"string": (tag==0x6)?"boolean": "unknown"); switch(tag) { case 0x0: { JSObject *obj = JSVAL_TO_OBJECT(vp); jsval val; if( JS_IsArrayObject(cx, obj) ) { jsuint len; JS_GetArrayLength(cx, obj, &len); func(" object is an array of %u elements", len); for(jsuint c = 0; c<len; c++) { func(" dumping element %u:",c); JS_GetElement(cx, obj, c, &val); if(val == JSVAL_VOID) func(" content is VOID"); else js_debug_argument(cx, val); } } else { func(" object type is unknown to us (not an array?)"); } } break; case 0x1: { jsint num = js_get_int(vp); func(" Sint[ %i ] Uint[ %u ] Double [ %.2f ]", num, num, num); } break; case 0x2: { jsdouble num = js_get_double(vp); func(" Double [ %.2f ]", num); } break; case 0x4: { char *cap; if(JSVAL_IS_STRING(vp)) { cap = JS_GetStringBytes( JS_ValueToString(cx, vp) ); func(" string is \"%s\"",cap); } else { JS_ReportError(cx,"%s: argument value is not a string",__FUNCTION__); ::error("%s: argument value is not a string",__FUNCTION__); } } break; case 0x6: { bool b = false; b = JSVAL_TO_BOOLEAN(vp); func(" boolean is %i",b); } break; default: func(" arg %u is unhandled, probably double"); jsint num = js_get_double(vp); func(" Double [ %.4f ] - Sint[ %i ] - Uint[ %u ]", num, num, num); } }
void js_debug_property(JSContext *cx, jsval vp) { func(" vp mem address %p", &vp); int tag = JSVAL_TAG(vp); func(" type tag is %i: %s",tag, (tag==0x0)?"object": (tag==0x1)?"integer": (tag==0x2)?"double": (tag==0x4)?"string": (tag==0x6)?"boolean": "unknown"); switch(tag) { case 0x0: { JSObject *obj = JSVAL_TO_OBJECT(vp); jsval val; if( JS_IsArrayObject(cx, obj) ) { jsuint len; JS_GetArrayLength(cx, obj, &len); func(" object is an array of %u elements", len); for(jsuint c = 0; c<len; c++) { func(" dumping element %u:",c); JS_GetElement(cx, obj, c, &val); if(val == JSVAL_VOID) func(" content is VOID"); else js_debug_property(cx, val); } } else { func(" object type is unknown to us (not an array?)"); } } break; case 0x1: { JS_PROP_INT(num, vp); func(" Sint[ %i ] Uint[ %u ]", num, num); } break; case 0x2: { JS_PROP_DOUBLE(num, vp); func(" double is %.4f",num); } break; case 0x4: { char *cap = NULL; JS_PROP_STRING(cap); func(" string is \"%s\"",cap); } break; case 0x6: { bool b = false; b = JSVAL_TO_BOOLEAN(vp); func(" boolean is %i",b); } break; default: func(" tag %u is unhandled, probably double"); JS_PROP_DOUBLE(num, vp); func(" Double [ %.4f ] - Sint[ %i ] - Uint[ %u ]", num, num, num); } }