Merge branch 'master' into fix-super

This commit is contained in:
Bob Nystrom
2015-05-27 06:57:20 -07:00
22 changed files with 434 additions and 297 deletions

63
test/api/main.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <string.h>
#include "io.h"
#include "vm.h"
#include "wren.h"
#include "return_bool/return_bool.h"
#include "return_double/return_double.h"
#include "return_null/return_null.h"
#define REGISTER_TEST(name) \
if (strcmp(testName, #name) == 0) return name##BindForeign(fullName)
// The name of the currently executing API test.
const char* testName;
static WrenForeignMethodFn bindForeign(
WrenVM* vm, const char* module, const char* className,
bool isStatic, const char* signature)
{
if (strcmp(module, "main") != 0) return NULL;
// For convenience, concatenate all of the method qualifiers into a single
// signature string.
char fullName[256];
fullName[0] = '\0';
if (isStatic) strcat(fullName, "static ");
strcat(fullName, className);
strcat(fullName, ".");
strcat(fullName, signature);
REGISTER_TEST(return_bool);
REGISTER_TEST(return_double);
REGISTER_TEST(return_null);
fprintf(stderr,
"Unknown foreign method '%s' for test '%s'\n", fullName, testName);
exit(1);
return NULL;
}
int main(int argc, const char* argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: wren <test>\n");
return 64; // EX_USAGE.
}
testName = argv[1];
// The test script is at "test/api/<test>/<test>.wren".
char testPath[256];
strcpy(testPath, "test/api/");
strcat(testPath, testName);
strcat(testPath, "/");
strcat(testPath, testName);
strcat(testPath, ".wren");
runFile(bindForeign, testPath);
return 0;
}

View File

@ -0,0 +1,21 @@
#include <string.h>
#include "return_bool.h"
static void returnTrue(WrenVM* vm)
{
wrenReturnBool(vm, true);
}
static void returnFalse(WrenVM* vm)
{
wrenReturnBool(vm, false);
}
WrenForeignMethodFn return_boolBindForeign(const char* signature)
{
if (strcmp(signature, "static Api.returnTrue") == 0) return returnTrue;
if (strcmp(signature, "static Api.returnFalse") == 0) return returnFalse;
return NULL;
}

View File

@ -0,0 +1,3 @@
#include "wren.h"
WrenForeignMethodFn return_boolBindForeign(const char* signature);

View File

@ -0,0 +1,7 @@
class Api {
foreign static returnTrue
foreign static returnFalse
}
IO.print(Api.returnTrue) // expect: true
IO.print(Api.returnFalse) // expect: false

View File

@ -0,0 +1,21 @@
#include <string.h>
#include "return_double.h"
static void returnInt(WrenVM* vm)
{
wrenReturnDouble(vm, 123456);
}
static void returnFloat(WrenVM* vm)
{
wrenReturnDouble(vm, 123.456);
}
WrenForeignMethodFn return_doubleBindForeign(const char* signature)
{
if (strcmp(signature, "static Api.returnInt") == 0) return returnInt;
if (strcmp(signature, "static Api.returnFloat") == 0) return returnFloat;
return NULL;
}

View File

@ -0,0 +1,3 @@
#include "wren.h"
WrenForeignMethodFn return_doubleBindForeign(const char* signature);

View File

@ -0,0 +1,7 @@
class Api {
foreign static returnInt
foreign static returnFloat
}
IO.print(Api.returnInt) // expect: 123456
IO.print(Api.returnFloat) // expect: 123.456

View File

@ -0,0 +1,15 @@
#include <string.h>
#include "return_null.h"
static void implicitNull(WrenVM* vm)
{
// Do nothing.
}
WrenForeignMethodFn return_nullBindForeign(const char* signature)
{
if (strcmp(signature, "static Api.implicitNull") == 0) return implicitNull;
return NULL;
}

View File

@ -0,0 +1,3 @@
#include "wren.h"
WrenForeignMethodFn return_nullBindForeign(const char* signature);

View File

@ -0,0 +1,5 @@
class Api {
foreign static implicitNull
}
IO.print(Api.implicitNull == null) // expect: true

View File

@ -14,37 +14,4 @@ class Foo {
IO.print([1, new Foo, 2]) // expect: [1, Foo.toString, 2]
// Lists that directly contain themselves.
var list = []
list.add(list)
IO.print(list) // expect: [...]
list = [1, 2]
list[0] = list
IO.print(list) // expect: [..., 2]
list = [1, 2]
list[1] = list
IO.print(list) // expect: [1, ...]
// Lists that indirectly contain themselves.
list = [null, [2, [3, null, 4], null, 5], 6]
list[0] = list
list[1][1][1] = list
list[1][2] = list
IO.print(list) // expect: [..., [2, [3, ..., 4], ..., 5], 6]
// List containing an object that calls toString on a recursive list.
class Box {
new(field) { _field = field }
toString { "box " + _field.toString }
}
list = [1, 2]
list.add(new Box(list))
IO.print(list) // expect: [1, 2, box ...]
// List containing a map containing the list.
list = [1, null, 2]
list[1] = {"list": list}
IO.print(list) // expect: [1, {list: ...}, 2]
// TODO: Handle lists that contain themselves.

View File

@ -24,28 +24,4 @@ IO.print(s == "{1: 2, 3: 4, 5: 6}" ||
s == "{5: 6, 1: 2, 3: 4}" ||
s == "{5: 6, 3: 4, 1: 2}") // expect: true
// Map that directly contains itself.
var map = {}
map["key"] = map
IO.print(map) // expect: {key: ...}
// Map that indirectly contains itself.
map = {}
map["a"] = {"b": {"c": map}}
IO.print(map) // expect: {a: {b: {c: ...}}}
// Map containing an object that calls toString on a recursive map.
class Box {
new(field) { _field = field }
toString { "box " + _field.toString }
}
map = {}
map["box"] = new Box(map)
IO.print(map) // expect: {box: box ...}
// Map containing a list containing the map.
map = {}
map["list"] = [1, map, 2]
IO.print(map) // expect: {list: [1, ..., 2]}
// TODO: Handle maps that contain themselves.