Constructing object literals.

git-svn-id: svn://10.0.0.236/trunk@126379 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
rogerl%netscape.com 2002-08-05 23:08:39 +00:00
parent 8f365056b7
commit 94098a015a
3 changed files with 49 additions and 4 deletions

View File

@ -77,6 +77,7 @@ namespace MetaData {
static void Multiname_finalize(JSContext *cx, JSObject *obj);
static void LexicalReference_finalize(JSContext *cx, JSObject *obj);
static void Namespace_finalize(JSContext *cx, JSObject *obj);
static void JS2Object_finalize(JSContext *cx, JSObject *obj);
JSClass gMonkeyMultinameClass =
{ "Multiname", JSCLASS_HAS_PRIVATE,
@ -97,6 +98,12 @@ namespace MetaData {
JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub,
JS_ResolveStub, JS_ConvertStub, Namespace_finalize };
JSClass gMonkeyJS2ObjectClass =
{ "JS2Object", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub,
JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub,
JS_ResolveStub, JS_ConvertStub, JS2Object_finalize };
// member functions at global scope
JSFunctionSpec jsfGlobal [] =
{
@ -243,6 +250,29 @@ namespace MetaData {
}
/******************************************************************************
JS2Object
******************************************************************************/
// finish constructing a JS2Object
static JSBool
JS2Object_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JS2Metadata *meta = static_cast<JS2Metadata *>(JS_GetContextPrivate(cx));
if (!JS_SetPrivate(cx, obj, meta->objectClass->construct()))
return JS_FALSE;
return JS_TRUE;
}
// finalize a Namespace - called by Monkey gc
static void
JS2Object_finalize(JSContext *cx, JSObject *obj)
{
ASSERT(OBJ_GET_CLASS(cx, obj) == &gMonkeyJS2ObjectClass);
DynamicInstance *di = static_cast<DynamicInstance *>(JS_GetPrivate(cx, obj));
if (di) delete di;
}
@ -280,6 +310,10 @@ namespace MetaData {
&gMonkeyNamespaceClass, Namespace_constructor, 0,
NULL, NULL, NULL, NULL);
JS_InitClass(monkeyContext, monkeyGlobalObject, NULL,
&gMonkeyJS2ObjectClass, JS2Object_constructor, 0,
NULL, NULL, NULL, NULL);
if (!JS_DefineFunctions(monkeyContext, monkeyGlobalObject, jsfGlobal))
throw "Monkey start failure";
}

View File

@ -527,6 +527,9 @@ namespace MetaData {
else
s += "false";
break;
case ExprNode::objectLiteral:
s += "new JS2Object()";
break;
default:
NOT_REACHED("Not Yet Implemented");
}

View File

@ -40,22 +40,25 @@
#include "parser.h"
#include <map>
#include <time.h>
namespace JavaScript {
namespace MetaData {
// forward definitions:
class JS2Object;
class JS2Metadata;
class JS2Class;
class StaticBinding;
class Environment;
class Context;
class Context;
typedef jsval js2val;
typedef void (Invokable)();
typedef Invokable Callor;
typedef Invokable Constructor;
typedef JS2Object *(Constructor)();
enum Phase { CompilePhase, RunPhase };
@ -285,6 +288,8 @@ public:
class JS2Class : public Frame {
public:
JS2Class() : Frame(Class) { }
StringAtom &getName();
InstanceBindingMap instanceReadBindings; // Map of qualified names to readable instance members defined in this class
InstanceBindingMap instanceWriteBindings; // Map of qualified names to writable instance members defined in this class
@ -302,8 +307,8 @@ public:
bool primitive; // true if this class was defined with the primitive attribute
bool final; // true if this class cannot be subclassed
Callor call; // A procedure to call when this class is used in a call expression
Constructor construct; // A procedure to call when this class is used in a new expression
Callor *call; // A procedure to call when this class is used in a call expression
Constructor *construct; // A procedure to call when this class is used in a new expression
};
@ -337,6 +342,8 @@ public:
// Instances of dynamic classes are represented as DYNAMICINSTANCE records. These instances can contain fixed and dynamic properties.
class DynamicInstance {
public:
DynamicInstance(JS2Class *type) : type(type), call(NULL), construct(NULL), env(NULL), typeofString(type->getName()) { }
JS2Class *type; // This instance's type
Invokable *call; // A procedure to call when this instance is used in a call expression
Invokable *construct; // A procedure to call when this instance is used in a new expression
@ -561,6 +568,7 @@ public:
JS2Class *numberClass;
JS2Class *characterClass;
JS2Class *stringClass;
JS2Class *objectClass;
Parser *mParser; // used for error reporting