fixed #1811: Adding a method to get file data for JSB. Updating the way of encoding cpp string to jsval or jsval to cpp string.

This commit is contained in:
James Chen 2013-03-08 14:34:59 +08:00
parent 4d8a735463
commit f7e60e290f
5 changed files with 9 additions and 27 deletions

View File

@ -459,7 +459,9 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c
js::RootedObject obj(cx, global); js::RootedObject obj(cx, global);
JS::CompileOptions options(cx); JS::CompileOptions options(cx);
options.setUTF8(true).setFileAndLine(rpath.c_str(), 1); options.setFileAndLine(rpath.c_str(), 1);
// Don't setUTF8 since it will cause messy string output by cc.log .
// options.setUTF8(true).setFileAndLine(rpath.c_str(), 1);
// this will always compile the script, we can actually check if the script // this will always compile the script, we can actually check if the script
// was compiled before, because it can be in the global map // was compiled before, because it can be in the global map
@ -1622,13 +1624,7 @@ JSBool JSBDebug_BufferWrite(JSContext* cx, unsigned argc, jsval* vp)
{ {
if (argc == 1) { if (argc == 1) {
jsval* argv = JS_ARGV(cx, vp); jsval* argv = JS_ARGV(cx, vp);
const char* str; JSStringWrapper strWrapper(argv[0]);
JSString* jsstr = JS_ValueToString(cx, argv[0]);
// Not supported in SpiderMonkey v19
//str = JS_EncodeString(cx, jsstr);
JSStringWrapper strWrapper(jsstr);
// this is safe because we're already inside a lock (from clearBuffers) // this is safe because we're already inside a lock (from clearBuffers)
outData.append(strWrapper.get()); outData.append(strWrapper.get());
} }

View File

@ -267,8 +267,7 @@ public:
} }
~JSStringWrapper() { ~JSStringWrapper() {
if (buffer) { if (buffer) {
// JS_free(ScriptingCore::getInstance()->getGlobalContext(), (void*)buffer); JS_free(ScriptingCore::getInstance()->getGlobalContext(), (void*)buffer);
CC_SAFE_DELETE_ARRAY(buffer);
} }
} }
void set(jsval val, JSContext* cx) { void set(jsval val, JSContext* cx) {
@ -283,14 +282,7 @@ public:
if (!cx) { if (!cx) {
cx = ScriptingCore::getInstance()->getGlobalContext(); cx = ScriptingCore::getInstance()->getGlobalContext();
} }
// Not suppored in SpiderMonkey v19 buffer = JS_EncodeString(cx, string);
//buffer = JS_EncodeString(cx, string);
const jschar *chars = JS_GetStringCharsZ(cx, string);
size_t l = JS_GetStringLength(string);
char* pUTF8Str = cc_utf16_to_utf8((const unsigned short*)chars, l, NULL, NULL);
buffer = pUTF8Str;
} }
std::string get() { std::string get() {
return buffer; return buffer;

View File

@ -1 +1 @@
e4b7d86461a17a555283bbc28cba2ab48cab21cd c12cdcc2ffa853b5b702ed1347279ff7fa076974

View File

@ -80,12 +80,8 @@ JSBool JSBCore_log(JSContext *cx, uint32_t argc, jsval *vp)
JSString *string = NULL; JSString *string = NULL;
JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &string); JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &string);
if (string) { if (string) {
// Not supported in SpiderMonkey v19 JSAutoByteString log_str(cx, string);
//char *cstr = JS_EncodeString(cx, string); CCLOG(log_str.ptr());
const jschar *chars = JS_GetStringCharsZ(cx, string);
size_t l = JS_GetStringLength(string);
char* pUTF8Str = cc_utf16_to_utf8((const unsigned short*)chars, l, NULL, NULL);
CCLOG(pUTF8Str);
} }
return JS_TRUE; return JS_TRUE;

View File

@ -199,8 +199,6 @@ JSBool jsval_to_charptr( JSContext *cx, jsval vp, const char **ret )
// root it // root it
vp = STRING_TO_JSVAL(jsstr); vp = STRING_TO_JSVAL(jsstr);
// Not supported in SpiderMonkey v19
//char *ptr = JS_EncodeString(cx, jsstr);
JSStringWrapper strWrapper(jsstr); JSStringWrapper strWrapper(jsstr);
// XXX: It is converted to CCString and then back to char* to autorelease the created object. // XXX: It is converted to CCString and then back to char* to autorelease the created object.