Subj : freeze and thaw script files To : netscape.public.mozilla.jseng From : frankin@miramar.com (Forest Rankin) Date : Mon Jun 16 2003 04:48 pm I am trying to test out compiling some script code, writting it to a file and then load it back in and execute it. Writting it to the file seems to be working, but executing the thaw'd code is giving me problems. The code in question is below. JSScript* pjsScript = JS_CompileScript( m_pContext, m_pGlobal, strScriptCode, strScriptCode.GetLength(), m_strScriptPath, 0); if (pjsScript == NULL) { return false; } // Create a script object that will own this script. JSObject* pScriptObject = JS_NewScriptObject(m_pContext, pjsScript); if (pScriptObject != NULL) { // flatten the "script object" to a string jsval frozen; JSBool okay = JS_CallFunctionName(m_pContext, pScriptObject, "freeze", 0, NULL, &frozen); if (okay) { JSString* pjstring = JSVAL_TO_STRING(frozen); char* pBuf = JS_GetStringBytes(pjstring); size_t len = JS_GetStringLength(pjstring); CFile fileCompiled; if (fileCompiled.Open(strCompiledPath.data(), CFile::modeReadWrite|CFile::modeCreate|CFile::typeBinary)) { fileCompiled.Write(pBuf, len); fileCompiled.Close(); } } } // now thaw it out. CFile fileCompiled; if (fileCompiled.Open(strCompiledPath.data(), CFile::modeRead|CFile::typeBinary)) { std::auto_ptr buffer(new char[fileCompiled.GetLength()]); char* pBuf = buffer.get(); size_t len = fileCompiled.Read(pBuf, (UINT)fileCompiled.GetLength()); fileCompiled.Close(); // convert deserialized script to a JSString JSString* pStr = JS_NewStringCopyN(m_pContext, pBuf, len); if (pStr != NULL) { // Create a script object that will own this script. JSObject* pScriptObject = JS_NewScriptObject(m_pContext, NULL); if (pScriptObject != NULL) { // Call the "thaw" method to put the "script" back in the script object. jsval argv[1]; argv[0] = STRING_TO_JSVAL(pStr); jsval result; JSBool okay = JS_CallFunctionName(m_pContext, pScriptObject, "thaw", 1, argv, &result); if (okay == JS_TRUE) { // Call the "exec" method to run the reconstituted script. Pass // the necessary parent object for the script argv[0] = OBJECT_TO_JSVAL(m_pGlobal); okay = JS_CallFunctionName(m_pContext, pScriptObject, "exec", 1, argv, &result); return (okay == JS_TRUE); } } } } I get a JS_TRUE from the JS_CallFunctionName to "exec" but then when I try to call "MyFunction()" on the script I get the below error. Script error in 'C:\test\Test.script', line: 1 ReferenceError: MyFunction is not defined Any Help would be appriciated. forest .