Bring things up to date with the new world order, including serialization
git-svn-id: svn://10.0.0.236/branches/DOM_AGNOSTIC_BRANCH@179889 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
@@ -35,6 +35,9 @@
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
#include "nsIScriptContext.h"
|
||||
#include "nsIScriptGlobalObject.h"
|
||||
#include "nsIObjectInputStream.h"
|
||||
#include "nsIObjectOutputStream.h"
|
||||
#include "nsITimelineService.h"
|
||||
#include "nsITimer.h"
|
||||
#include "nsIArray.h"
|
||||
#include "prtime.h"
|
||||
@@ -43,6 +46,7 @@
|
||||
#include "nsPyContext.h"
|
||||
|
||||
#include "compile.h"
|
||||
#include "marshal.h"
|
||||
#include "eval.h"
|
||||
|
||||
static PRInt32 sContextCount;
|
||||
@@ -121,8 +125,8 @@ NS_IMPL_RELEASE(nsPythonContext)
|
||||
/*static*/ nsresult nsPythonContext::HandlePythonError()
|
||||
{
|
||||
// need to raise an exception
|
||||
NS_WARNING("Python error");
|
||||
PyErr_Print(); // for now.
|
||||
NS_WARNING("Python error calling DOM script");
|
||||
PyXPCOM_LogError("Python error");
|
||||
return PyXPCOM_SetCOMErrorFromPyException();
|
||||
}
|
||||
|
||||
@@ -264,6 +268,7 @@ nsPythonContext::CompileScript(const PRUnichar* aText,
|
||||
void** aScriptObject)
|
||||
{
|
||||
NS_ENSURE_TRUE(mIsInitialized, NS_ERROR_NOT_INITIALIZED);
|
||||
NS_TIMELINE_MARK_FUNCTION("nsPythonContext::CompileScript");
|
||||
|
||||
// XXX - todo - specify PyCF_SOURCE_IS_UTF8 in Py_CompileStringFlags
|
||||
NS_ConvertUTF16toUTF8 cs(aText, aTextLength);
|
||||
@@ -482,6 +487,69 @@ nsPythonContext::InitClasses(void *aGlobalObj)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsPythonContext::Serialize(nsIObjectOutputStream* aStream, void *aScriptObject)
|
||||
{
|
||||
NS_TIMELINE_MARK_FUNCTION("nsPythonContext::Serialize");
|
||||
CEnterLeavePython _celp;
|
||||
nsresult rv;
|
||||
if (!PyCode_Check((PyObject *)aScriptObject)) {
|
||||
NS_ERROR("aScriptObject is not a code object");
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
rv = aStream->Write32(PyImport_GetMagicNumber());
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
PyObject *obMarshal =
|
||||
#ifdef Py_MARSHAL_VERSION
|
||||
PyMarshal_WriteObjectToString((PyObject *)aScriptObject,
|
||||
Py_MARSHAL_VERSION);
|
||||
#else
|
||||
// 2.3 etc - only takes 1 arg.
|
||||
PyMarshal_WriteObjectToString((PyObject *)aScriptObject);
|
||||
|
||||
#endif
|
||||
if (!obMarshal)
|
||||
return HandlePythonError();
|
||||
NS_ASSERTION(PyString_Check(obMarshal), "marshal returned a non string?");
|
||||
rv |= aStream->Write32(PyString_GET_SIZE(obMarshal));
|
||||
rv |= aStream->WriteBytes(PyString_AS_STRING(obMarshal), PyString_GET_SIZE(obMarshal));
|
||||
Py_DECREF(obMarshal);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsPythonContext::Deserialize(nsIObjectInputStream* aStream, void **aResult)
|
||||
{
|
||||
NS_TIMELINE_MARK_FUNCTION("nsPythonContext::Deserialize");
|
||||
nsresult rv;
|
||||
PRUint32 magic;
|
||||
rv = aStream->Read32(&magic);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
if (magic != PyImport_GetMagicNumber()) {
|
||||
NS_WARNING("Python has different marshal version");
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
PRUint32 nBytes;
|
||||
rv = aStream->Read32(&nBytes);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
char* data = nsnull;
|
||||
rv = aStream->ReadBytes(nBytes, &data);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
CEnterLeavePython _celp;
|
||||
PyObject *codeObject = PyMarshal_ReadObjectFromString(data, nBytes);
|
||||
if (data)
|
||||
nsMemory::Free(data);
|
||||
if (codeObject == NULL)
|
||||
return HandlePythonError();
|
||||
NS_ASSERTION(PyCode_Check(codeObject), "unmarshal returned a non string?");
|
||||
*aResult = codeObject;
|
||||
PYLEAK_STAT_INCREMENT(ScriptObject);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
nsPythonContext::SetDefaultLanguageVersion(PRUint32 aVersion)
|
||||
{
|
||||
|
||||
@@ -143,6 +143,9 @@ public:
|
||||
virtual void WillInitializeContext();
|
||||
virtual void DidInitializeContext();
|
||||
|
||||
virtual nsresult Serialize(nsIObjectOutputStream* aStream, void *aScriptObject);
|
||||
virtual nsresult Deserialize(nsIObjectInputStream* aStream, void **aResult);
|
||||
|
||||
NS_DECL_NSITIMERCALLBACK
|
||||
protected:
|
||||
PRPackedBool mIsInitialized;
|
||||
|
||||
@@ -91,11 +91,6 @@ nsPythonRuntime::RegisterSelf(nsIComponentManager* aCompMgr,
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
// And also register a short name.
|
||||
rv = catman->AddCategoryEntry(SCRIPT_LANGUAGE_CATEGORY,
|
||||
"python",
|
||||
NS_SCRIPT_LANGUAGE_PYTHON_CONTRACTID,
|
||||
PR_TRUE, PR_TRUE, getter_Copies(previous));
|
||||
return rv;
|
||||
}
|
||||
|
||||
@@ -116,11 +111,25 @@ nsPythonRuntime::UnregisterSelf(nsIComponentManager* aCompMgr,
|
||||
|
||||
catman->DeleteCategoryEntry(SCRIPT_LANGUAGE_CATEGORY,
|
||||
"application/x-python", PR_TRUE);
|
||||
catman->DeleteCategoryEntry(SCRIPT_LANGUAGE_CATEGORY,
|
||||
"python", PR_TRUE);
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsPythonRuntime::LockGCThing(void *object)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsPythonRuntime::UnlockGCThing(void *object)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// An "implicit" Python module.
|
||||
//
|
||||
PyObject *PyJSExec(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *obGlobal;
|
||||
|
||||
@@ -77,4 +77,7 @@ public:
|
||||
*flags=0;
|
||||
return NS_OK;
|
||||
}
|
||||
virtual nsresult LockGCThing(void *object);
|
||||
virtual nsresult UnlockGCThing(void *object);
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user