"count" method on strings.

This commit is contained in:
Bob Nystrom
2013-10-26 14:34:15 -07:00
parent f867bb8348
commit d9a4da94a3

View File

@ -38,6 +38,7 @@ static void push(Fiber* fiber, Value value);
static Value pop(Fiber* fiber);
static Value primitive_metaclass_new(Value receiver);
static Value primitive_num_abs(Value receiver);
static Value primitive_string_count(Value receiver);
VM* newVM()
{
@ -62,6 +63,9 @@ VM* newVM()
vm->numClass = makeClass();
PRIMITIVE(num, abs);
vm->stringClass = makeClass();
PRIMITIVE(string, count);
return vm;
}
@ -449,3 +453,11 @@ Value primitive_num_abs(Value receiver)
return (Value)makeNum(value);
}
Value primitive_string_count(Value receiver)
{
double count = strlen(((ObjString*)receiver)->value);
return (Value)makeNum(count);
}