1
0
forked from Mirror/wren

Adds a Meta library with an eval function for interpreting code inline.

This commit is contained in:
Gavin Schulz
2015-03-22 14:16:53 -07:00
parent f2c5c804a4
commit a937bd1cf9
7 changed files with 60 additions and 1 deletions

1
builtin/meta.wren Normal file
View File

@ -0,0 +1 @@
class Meta {}

View File

@ -243,7 +243,7 @@ def run_test(path):
print('')
for dir in ['core', 'io', 'language', 'limit']:
for dir in ['core', 'io', 'language', 'limit', 'meta']:
walk(join(WREN_DIR, 'test', dir), run_test)
print_line()

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.

21
src/vm/wren_meta.c Normal file
View File

@ -0,0 +1,21 @@
#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);
wrenInterpret(vm, "Meta", source);
}
void wrenLoadMetaLibrary(WrenVM* vm)
{
wrenInterpret(vm, "", libSource);
wrenDefineStaticMethod(vm, "Meta", "eval(_)", metaEval);
}
#endif

18
src/vm/wren_meta.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef wren_meta_h
#define wren_meta_h
#include <stdio.h>
#include "wren.h"
#include "wren_common.h"
#include "wren_value.h"
#include "wren_vm.h"
// This module defines the Meta class and its associated methods.
#if WREN_USE_LIB_META
void wrenLoadMetaLibrary(WrenVM* vm);
#endif
#endif

View File

@ -14,6 +14,10 @@
#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
@ -75,6 +79,9 @@ WrenVM* wrenNewVM(WrenConfiguration* configuration)
#if WREN_USE_LIB_IO
wrenLoadIOLibrary(vm);
#endif
#if WREN_USE_LIB_META
wrenLoadMetaLibrary(vm);
#endif
return vm;
}

View File

@ -0,0 +1,5 @@
var y
Meta.eval("y = 2")
IO.print(y) // expect: 2