more jsI?Context stuff, some Scriptable hackery

This commit is contained in:
shaver 1998-07-15 16:59:47 +00:00
Родитель 671a19cd9d
Коммит e8ab388331
7 изменённых файлов: 187 добавлений и 68 удалений

Просмотреть файл

@ -42,10 +42,10 @@ ErrorReporterHandler(JSContext *cx, const char* message,
if (!icx)
/* well, we can't exactly report an error... */
return;
str = JS_NewStringCopyZ(cx, message);
str = icx->newStringCopyZ(message);
if (!str)
return;
icx->reporter->reportError(str, report);
icx->reporter->reportError(icx, str, report);
}
jsContext::jsContext (JSRuntime *rt, uintN stacksize)
@ -205,7 +205,7 @@ jsContext::evaluateString(jsIScriptable *scope,
uintN lineno,
jsval *rval)
{
JSObject *obj = ::jsScriptableCreateJSObjectProxy(this, scope);
JSObject *obj = scope->getJSObject(this);
if (!obj)
return NS_ERROR_FAILURE;
if (!JS_EvaluateScript(cx, obj, JS_GetStringBytes(source),
@ -220,7 +220,12 @@ jsContext::evaluateString(jsIScriptable *scope,
nsresult
jsContext::initStandardObjects(jsIScriptable *scope)
{
return NS_ERROR_NOT_IMPLEMENTED;
JSObject *obj = scope->getJSObject(this);
if (!obj)
return NS_ERROR_FAILURE;
if (!JS_InitStandardClasses(cx, obj))
return NS_ERROR_FAILURE;
return NS_OK;
}
void
@ -228,7 +233,7 @@ jsContext::reportWarning(JSString *message)
{
/* XXX fill in report, make varargs */
if (reporter)
reporter->reportWarning(message, NULL);
reporter->reportWarning(this, message, NULL);
};
void
@ -277,3 +282,57 @@ jsContext::exit()
(void)JS_ClearContextThread(cx);
#endif
}
JSString *
jsContext::newStringCopyZ(const char *string)
{
return JS_NewStringCopyZ(cx, string);
}
JSString *
jsContext::newUCStringCopyZ(const jschar *string)
{
return JS_NewUCStringCopyZ(cx, string);
}
JSString *
jsContext::newStringCopyN(const char *string, size_t len)
{
return JS_NewStringCopyN(cx, string, len);
}
JSString *
jsContext::newUCStringCopyN(const jschar *string, size_t len)
{
return JS_NewUCStringCopyN(cx, string, len);
}
JSString *
jsContext::newString(char *string, size_t len)
{
return JS_NewString(cx, string, len);
}
JSString *
jsContext::newUCString(jschar *string, size_t len)
{
return JS_NewUCString(cx, string, len);
}
JSBool
jsContext::addRoot(void *root)
{
return JS_AddRoot(cx, root);
}
JSBool
jsContext::addNamedRoot(void *root, const char *name)
{
return JS_AddNamedRoot(cx, root, name);
}
JSBool
jsContext::removeRoot(void *root)
{
return JS_RemoveRoot(cx, root);
}

Просмотреть файл

@ -62,6 +62,17 @@ public:
void enter(void);
void exit(void);
JSString *newStringCopyZ(const char *string);
JSString *newUCStringCopyZ(const jschar *string);
JSString *newStringCopyN(const char *string, size_t len);
JSString *newUCStringCopyN(const jschar *string, size_t len);
JSString *newString(char *string, size_t len);
JSString *newUCString(jschar *string, size_t len);
JSBool addRoot(void *root);
JSBool addNamedRoot(void *root, const char *name);
JSBool removeRoot(void *root);
friend class jsRuntime;
friend class jsScriptable;
friend void ErrorReporterHandler(JSContext *cx, const char *message,

Просмотреть файл

@ -185,7 +185,17 @@ class jsIContext: public nsISupports {
* Break the Context-thread association.
*/
virtual void exit(void) = 0;
virtual JSString *newStringCopyZ(const char *string) = 0;
virtual JSString *newUCStringCopyZ(const jschar *string) = 0;
virtual JSString *newStringCopyN(const char *string, size_t len) = 0;
virtual JSString *newUCStringCopyN(const jschar *string, size_t len) = 0;
virtual JSString *newString(char *string, size_t len) = 0;
virtual JSString *newUCString(jschar *string, size_t len) = 0;
virtual JSBool addRoot(void *root) = 0;
virtual JSBool addNamedRoot(void *root, const char *name) = 0;
virtual JSBool removeRoot(void *root) = 0;
};
#endif /* JS_ICONTEXT_H */

Просмотреть файл

@ -28,21 +28,25 @@ extern "C" {
#include <jsapi.h>
}
#define JS_IERRORREPORTER_IID \
{ 0, 0, 0, \
{0, 0, 0, 0, 0, 0, 0, 0}}
class jsIErrorReporter: public nsISupports {
public:
jsIErrorReporter();
virtual ~jsIErrorReporter() = 0;
/**
* Report a warning.
*/
NS_IMETHOD reportWarning(JSString *message,
JSErrorReport *report) = 0;
virtual void reportWarning(jsIContext *cx,
JSString *message,
JSErrorReport *report) = 0;
/**
* Report an error.
*/
NS_IMETHOD reportError(JSString *message,
JSErrorReport *report) = 0;
virtual void reportError(jsIContext *cx,
JSString *message,
JSErrorReport *report) = 0;
};

Просмотреть файл

@ -37,21 +37,20 @@ class jsIScriptable; /* for mutually-dependent includes */
class jsIScriptable: public nsISupports {
public:
virtual JSString *getClassName() = 0;
virtual JSString *getClassName(jsIContext *) = 0;
/* XXX use jsid for name/index? */
virtual jsval get(jsval id) = 0;
virtual JSBool has(jsval id) = 0;
virtual void put(jsval id, jsval v) = 0;
virtual void del(jsval id) = 0;
NS_IMETHOD get(jsIContext *cx, const char *name, jsval *vp) = 0;
NS_IMETHOD has(jsIContext *cx, jsval id, JSBool *bp) = 0;
NS_IMETHOD put(jsIContext *cx, const char *name, jsval v) = 0;
NS_IMETHOD del(jsIContext *cx, jsval id) = 0;
virtual jsIScriptable *getPrototype() = 0;
virtual void setPrototype(jsIScriptable *prototype) = 0;
virtual jsIScriptable *getParentScope() = 0;
virtual void setParentScope(jsIScriptable *parent) = 0;
virtual jsIScriptable *getPrototype(jsIContext *cx) = 0;
NS_IMETHOD setPrototype(jsIContext *cx, jsIScriptable *prototype) = 0;
virtual jsIScriptable *getParentScope(jsIContext *cx) = 0;
NS_IMETHOD setParentScope(jsIContext *cx, jsIScriptable *parent) = 0;
/* virtual JSIdArray *getIds(); */
virtual jsval getDefaultValue(JSType hint) = 0;
virtual JSBool hasInstance(jsIScriptable *instance) = 0;
NS_IMETHOD getDefaultValue(jsIContext *cx, JSType hint, jsval *vp) = 0;
/**
* Return a classic JSAPI JSObject * for this object.
@ -66,7 +65,7 @@ class jsIScriptable: public nsISupports {
* Context is provided for GC rooting and other tasks. Be sure
* to unroot the proxy in your destructor, etc.
*/
virtual void setJSObject(jsIContext *, JSObject *)= 0;
NS_IMETHOD setJSObject(jsIContext *, JSObject *)= 0;
};
#endif /* JS_ISCRIPTABLE_H */

Просмотреть файл

@ -23,17 +23,43 @@ static NS_DEFINE_IID(kIScriptableIID, JS_ISCRIPTABLE_IID);
NS_IMPL_ISUPPORTS(jsScriptable, kIScriptableIID);
void
jsScriptable::init(jsIContext *icx, JSObject *object)
{
jsContext *cx = (jsContext *)icx;
#if 0
/* can't do this yet. see comment in ~jsScriptable. */
cx->addRoot(&obj);
cx->addRoot(&className);
#endif
setJSObject(cx, object);
}
jsScriptable::jsScriptable(jsIContext *cx, JSObject *object)
{
/* cx->addRoot(&obj); */
/* cx->addRoot(&className); */
setJSObject(cx, object);
init(cx, object);
}
jsScriptable::jsScriptable(jsIContext *cx, JSClass *clasp)
{
jsContext *icx = (jsContext *)cx;
JSObject *obj = JS_NewObject(icx->cx, clasp, NULL, NULL);
init(cx, obj);
}
jsScriptable::~jsScriptable()
{
/* cx->removeRoot(&obj); */
/* cx->removeRoot(&className); */
/* we need to remove the root, but we don't want to cache a
* JSContext *, since that JSContext could easily go away.
* We may need to reach into JSContext->rt and cache that, and
* then find a context on that runtime to use for the call to
* JS_RemoveRoot. What do we do if there aren't any contexts on
* that runtime at destructor time? Do we create one?
*/
#if 0
cx->removeRoot(&obj);
cx->removeRoot(&className);
#endif
}
JSObject *jsScriptable::getJSObject(jsIContext *cx)
@ -41,7 +67,7 @@ JSObject *jsScriptable::getJSObject(jsIContext *cx)
return obj;
}
void
nsresult
jsScriptable::setJSObject(jsIContext *cx, JSObject *object)
{
obj = object;
@ -56,70 +82,77 @@ jsScriptable::setJSObject(jsIContext *cx, JSObject *object)
->name);
/* XXX update proto and parent */
return NS_OK;
}
JSString *
jsScriptable::getClassName()
jsScriptable::getClassName(jsIContext *cx)
{
return className;
}
jsval
jsScriptable::get(jsval id)
nsresult
jsScriptable::get(jsIContext *cx, const char *name, jsval *vp)
{
return JSVAL_VOID;
jsContext *icx = (jsContext *)cx;
if (!JS_GetProperty(icx->cx, obj, name, vp)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
JSBool
jsScriptable::has(jsval id)
nsresult
jsScriptable::has(jsIContext *cx, jsval id, JSBool *bp)
{
return JS_FALSE;
return NS_ERROR_NOT_IMPLEMENTED;
}
void
jsScriptable::put(jsval id, jsval v)
nsresult
jsScriptable::put(jsIContext *cx, const char *name, jsval v)
{
jsContext *icx = (jsContext *)cx;
if (!JS_SetProperty(icx->cx, obj, name, &v)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
void
jsScriptable::del(jsval id)
nsresult
jsScriptable::del(jsIContext *cx, jsval id)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
jsIScriptable *
jsScriptable::getPrototype()
jsScriptable::getPrototype(jsIContext *cx)
{
return proto;
}
void
jsScriptable::setPrototype(jsIScriptable *prototype)
nsresult
jsScriptable::setPrototype(jsIContext *cx, jsIScriptable *prototype)
{
proto = prototype;
/* set the prototype of the underlying JSObject * */
return NS_OK;
}
jsIScriptable *
jsScriptable::getParentScope()
jsScriptable::getParentScope(jsIContext *cx)
{
return parent;
}
void
jsScriptable::setParentScope(jsIScriptable *parent)
nsresult
jsScriptable::setParentScope(jsIContext *cx, jsIScriptable *parent)
{
this->parent = parent;
/* set parent of underlying JSObject */
return NS_OK;
}
jsval
jsScriptable::getDefaultValue(JSType hint)
nsresult
jsScriptable::getDefaultValue(jsIContext *cx, JSType hint, jsval *vp)
{
return JSVAL_VOID;
}
JSBool
jsScriptable::hasInstance(jsIScriptable *instance)
{
return JS_FALSE;
return NS_ERROR_NOT_IMPLEMENTED;
}

Просмотреть файл

@ -28,28 +28,31 @@ class jsScriptable: public jsIScriptable {
JSObject *obj;
JSString *className;
jsIScriptable *proto, *parent;
void init(jsIContext *, JSObject *);
public:
NS_DECL_ISUPPORTS;
jsScriptable(jsIContext *, JSClass *);
jsScriptable(jsIContext *, JSObject *);
~jsScriptable();
JSString *getClassName();
JSString *getClassName(jsIContext *);
/* XXX use jsid for name/index? */
jsval get(jsval id);
JSBool has(jsval id);
void put(jsval id, jsval v);
void del(jsval id);
nsresult get(jsIContext *cx, const char *name, jsval *vp);
nsresult has(jsIContext *cx, jsval id, JSBool *bp);
nsresult put(jsIContext *cx, const char *name, jsval v);
nsresult del(jsIContext *cx, jsval id);
jsIScriptable *getPrototype();
void setPrototype(jsIScriptable *prototype);
jsIScriptable *getParentScope();
void setParentScope(jsIScriptable *parent);
jsIScriptable *getPrototype(jsIContext *cx);
nsresult setPrototype(jsIContext *cx, jsIScriptable *prototype);
jsIScriptable *getParentScope(jsIContext *cx);
nsresult setParentScope(jsIContext *cx, jsIScriptable *parent);
/* JSIdArray *getIds(); */
jsval getDefaultValue(JSType hint);
JSBool hasInstance(jsIScriptable *instance);
nsresult getDefaultValue(jsIContext *cx, JSType hint, jsval *vp);
/* JSBool hasInstance(jsIContext *cx, jsIScriptable *instance); */
JSObject *getJSObject(jsIContext *);
void setJSObject(jsIContext *, JSObject *);
nsresult setJSObject(jsIContext *, JSObject *);
};
#endif /* JS_SCRIPTABLE_H */