JSBool JsGlobal::convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp,const char * const objName) { static log4cplus::Logger log = log4cplus::Logger::getInstance("fsm.JsContext.convert"); //fsm::env::Js::ToString objString(cx,OBJECT_TO_JSVAL(obj)); LOG4CPLUS_TRACE(log, "converting ["<< objName <<"] to "<< JS_GetTypeName(cx, type) <<" type"); return JS_TRUE; }
static JSBool convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp) { if (resolverHasMethod(cx, obj, "convert")) { JSString *typeStr = JS_NewStringCopyZ(cx, JS_GetTypeName(cx, type)); jsval args[1]; args[0] = STRING_TO_JSVAL(typeStr); return delegateToResolver(cx, obj, "convert", 1, args, vp); } return JS_ConvertStub(cx, obj, type, vp); }
/* * Log a given jsval with a prefix. * cx - JSContext for the JS execution context to use * val - jsval to print * prefix - string to print before the value, defaults to empty string * * TODO(jat): this whole printf-style logging needs to be replaced, but we * run into library version issues if we use C++ iostreams so we would need * to implement our own equivalent. Given that this code is all likely to * be rewritten for out-of-process hosted mode, it seems unlikely to be worth * the effort until that is completed. */ void PrintJSValue(JSContext* cx, jsval val, char* prefix="") { JSType type = JS_TypeOfValue(cx, val); const char* typeString=JS_GetTypeName(cx, type); static const int BUF_SIZE = 256; char buf[BUF_SIZE]; const char *bufEnd = buf + BUF_SIZE; char* p = buf; p += append_sprintf(p, bufEnd, "%s%s", prefix, typeString); switch(type) { case JSTYPE_VOID: break; case JSTYPE_BOOLEAN: p += append_sprintf(p, bufEnd, ": %s", JSVAL_TO_BOOLEAN(val) ? "true" : "false"); break; case JSTYPE_NUMBER: if (JSVAL_IS_INT(val)) { p += append_sprintf(p, bufEnd, ": %d", JSVAL_TO_INT(val)); } else { p += append_sprintf(p, bufEnd, ": %lf", (double)*JSVAL_TO_DOUBLE(val)); } break; case JSTYPE_OBJECT: { JSObject* obj = JSVAL_TO_OBJECT(val); if (!JSVAL_IS_OBJECT(val)) break; JSClass* clazz = obj ? JS_GET_CLASS(cx, obj) : 0; p += append_sprintf(p, bufEnd, " @ %08x, class %s", (unsigned)obj, clazz ? clazz->name : "<null>"); break; } case JSTYPE_FUNCTION: case JSTYPE_LIMIT: break; case JSTYPE_STRING: { /* * TODO(jat): support JS strings with international characters */ JsStringWrap str(cx, JSVAL_TO_STRING(val)); p += append_sprintf(p, bufEnd, ": %.*s", str.length(), str.bytes()); break; } } Tracer::log("%s", buf); }
////////////////////////////////////////////////////////////////////////// // constructor JSBool File_constructor(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { File_data * data = (File_data *)malloc(sizeof(File_data)); memset(data, 0, sizeof(File_data)); JSObject * propertyMap = 0; if(argc==1) // File(path) or File(propertyMap) { if(!JSVAL_IS_STRING(argv[0]) && !JSVAL_IS_OBJECT(argv[0])) { JS_ReportError(cx, "File constructor argument #1 has an unexpected type. Expected string or object, got: %s", JS_GetTypeName(cx, JS_TypeOfValue(cx, argv[0]))); return JS_FALSE; } if(JSVAL_IS_OBJECT(argv[0])) JS_ValueToObject(cx, argv[0], &propertyMap); else if(JSVAL_IS_STRING(argv[0])) JS_SetProperty(cx, obj, "path", &argv[0]); } else if(argc==2) // File(path, propertyMap) { if(!JSVAL_IS_STRING(argv[0])) { JS_ReportError(cx, "File constructor argument #1 has an unexpected type. Expected string, got: %s", JS_GetTypeName(cx, JS_TypeOfValue(cx, argv[0]))); return JS_FALSE; } else if(!JSVAL_IS_OBJECT(argv[1])) { JS_ReportError(cx, "File constructor argument #2 has an unexpected type. Expected object, got: %s", JS_GetTypeName(cx, JS_TypeOfValue(cx, argv[1]))); return JS_FALSE; } JS_SetProperty(cx, obj, "path", &argv[0]); JS_ValueToObject(cx, argv[1], &propertyMap); } if(propertyMap && !JS_CopyObjectProperties(cx, propertyMap, obj)) return JS_FALSE; JS_SetPrivate(cx, obj, data); return JS_TRUE; }