1
0
forked from Mirror/wren

Support "new" on lists.

This commit is contained in:
Bob Nystrom
2014-04-08 07:31:23 -07:00
parent 49feeae99c
commit f6dd88937b
3 changed files with 15 additions and 5 deletions

View File

@ -358,6 +358,11 @@ DEF_NATIVE(fn_toString)
RETURN_VAL(wrenNewString(vm, "<fn>", 4));
}
DEF_NATIVE(list_instantiate)
{
RETURN_OBJ(wrenNewList(vm, 0));
}
DEF_NATIVE(list_add)
{
ObjList* list = AS_LIST(args[0]);
@ -936,8 +941,6 @@ void wrenInitializeCore(WrenVM* vm)
// Define the methods specific to Class after wiring up its superclass to
// prevent the inherited ones from overwriting them.
// TODO: Now that instantiation is controlled by the class, implement "new"
// for List.
NATIVE(vm->classClass, " instantiate", class_instantiate);
NATIVE(vm->classClass, "name", class_name);
@ -1041,6 +1044,7 @@ void wrenInitializeCore(WrenVM* vm)
wrenInterpret(vm, "Wren core library", libSource);
vm->listClass = AS_CLASS(findGlobal(vm, "List"));
NATIVE(vm->listClass->metaclass, " instantiate", list_instantiate);
NATIVE(vm->listClass, "add ", list_add);
NATIVE(vm->listClass, "clear", list_clear);
NATIVE(vm->listClass, "count", list_count);

View File

@ -616,7 +616,7 @@ void wrenPrintValue(Value value);
// Returns true if [value] is a bool. Do not call this directly, instead use
// [IS_BOOL].
inline bool wrenIsBool(Value value)
static inline bool wrenIsBool(Value value)
{
#if WREN_NAN_TAGGING
return value.bits == TRUE_VAL.bits || value.bits == FALSE_VAL.bits;
@ -627,13 +627,13 @@ inline bool wrenIsBool(Value value)
// Returns true if [value] is an object of type [type]. Do not call this
// directly, instead use the [IS___] macro for the type in question.
inline bool wrenIsObjType(Value value, ObjType type)
static inline bool wrenIsObjType(Value value, ObjType type)
{
return IS_OBJ(value) && AS_OBJ(value)->type == type;
}
// Converts the raw object pointer [obj] to a [Value].
inline Value wrenObjectToValue(Obj* obj)
static inline Value wrenObjectToValue(Obj* obj)
{
#if WREN_NAN_TAGGING
return (Value)(SIGN_BIT | QNAN | (uint64_t)(obj));

6
test/list/new.wren Normal file
View File

@ -0,0 +1,6 @@
var list = new List
IO.print(list.count) // expect: 0
IO.print(list) // expect: []
list.add(1)
IO.print(list) // expect: [1]