Merge branch 'master' into foreign-methods

This commit is contained in:
Bob Nystrom
2015-03-25 20:06:55 -07:00
11 changed files with 115 additions and 37 deletions
+10 -11
View File
@@ -14,20 +14,19 @@ typedef struct WrenMethod WrenMethod;
// A generic allocation function that handles all explicit memory management
// used by Wren. It's used like so:
//
// - To allocate new memory, [memory] is NULL and [oldSize] is zero. It should
// return the allocated memory or NULL on failure.
// - To allocate new memory, [memory] is NULL and [newSize] is the desired
// size. It should return the allocated memory or NULL on failure.
//
// - To attempt to grow an existing allocation, [memory] is the memory,
// [oldSize] is its previous size, and [newSize] is the desired size.
// It should return [memory] if it was able to grow it in place, or a new
// pointer if it had to move it.
// - To attempt to grow an existing allocation, [memory] is the memory, and
// [newSize] is the desired size. It should return [memory] if it was able to
// grow it in place, or a new pointer if it had to move it.
//
// - To shrink memory, [memory], [oldSize], and [newSize] are the same as above
// but it will always return [memory].
// - To shrink memory, [memory] and [newSize] are the same as above but it will
// always return [memory].
//
// - To free memory, [memory] will be the memory to free and [newSize] and
// [oldSize] will be zero. It should return NULL.
typedef void* (*WrenReallocateFn)(void* memory, size_t oldSize, size_t newSize);
// - To free memory, [memory] will be the memory to free and [newSize] will be
// zero. It should return NULL.
typedef void* (*WrenReallocateFn)(void* memory, size_t newSize);
// A function callable from Wren code, but implemented in C.
typedef void (*WrenForeignMethodFn)(WrenVM* vm);
+7
View File
@@ -51,6 +51,13 @@
#define WREN_USE_LIB_IO 1
#endif
// If true, loads the "Meta" class in the standard library.
//
// Defaults to on.
#ifndef WREN_USE_LIB_META
#define WREN_USE_LIB_META 1
#endif
// These flags are useful for debugging and hacking on Wren itself. They are not
// intended to be used for production code. They default to off.
+22
View File
@@ -0,0 +1,22 @@
#include "wren_meta.h"
#if WREN_USE_LIB_META
// This string literal is generated automatically from meta.wren. Do not edit.
static const char* libSource =
"class Meta {}\n";
void metaEval(WrenVM* vm)
{
const char* source = wrenGetArgumentString(vm, 1);
// TODO: Type check argument.
wrenInterpret(vm, "Meta", source);
}
void wrenLoadMetaLibrary(WrenVM* vm)
{
wrenInterpret(vm, "", libSource);
wrenDefineStaticMethod(vm, "Meta", "eval(_)", metaEval);
}
#endif
+14
View File
@@ -0,0 +1,14 @@
#ifndef wren_meta_h
#define wren_meta_h
#include "wren.h"
#include "wren_common.h"
// This module defines the Meta class and its associated methods.
#if WREN_USE_LIB_META
void wrenLoadMetaLibrary(WrenVM* vm);
#endif
#endif
+10 -10
View File
@@ -14,26 +14,23 @@
#include "wren_io.h"
#endif
#if WREN_USE_LIB_META
#include "wren_meta.h"
#endif
#if WREN_DEBUG_TRACE_MEMORY || WREN_DEBUG_TRACE_GC
#include <time.h>
#endif
// The built-in reallocation function used when one is not provided by the
// configuration.
static void* defaultReallocate(void* memory, size_t oldSize, size_t newSize)
{
return realloc(memory, newSize);
}
WrenVM* wrenNewVM(WrenConfiguration* configuration)
{
WrenReallocateFn reallocate = defaultReallocate;
WrenReallocateFn reallocate = realloc;
if (configuration->reallocateFn != NULL)
{
reallocate = configuration->reallocateFn;
}
WrenVM* vm = (WrenVM*)reallocate(NULL, 0, sizeof(*vm));
WrenVM* vm = (WrenVM*)reallocate(NULL, sizeof(*vm));
memset(vm, 0, sizeof(WrenVM));
vm->reallocate = reallocate;
@@ -80,6 +77,9 @@ WrenVM* wrenNewVM(WrenConfiguration* configuration)
#if WREN_USE_LIB_IO
wrenLoadIOLibrary(vm);
#endif
#if WREN_USE_LIB_META
wrenLoadMetaLibrary(vm);
#endif
return vm;
}
@@ -214,7 +214,7 @@ void* wrenReallocate(WrenVM* vm, void* memory, size_t oldSize, size_t newSize)
if (newSize > 0 && vm->bytesAllocated > vm->nextGC) collectGarbage(vm);
#endif
return vm->reallocate(memory, oldSize, newSize);
return vm->reallocate(memory, newSize);
}
// Captures the local variable [local] into an [Upvalue]. If that local is