forked from Mirror/wren
Adds a Meta library with an eval function for interpreting code inline.
This commit is contained in:
1
builtin/meta.wren
Normal file
1
builtin/meta.wren
Normal file
@ -0,0 +1 @@
|
||||
class Meta {}
|
||||
@ -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()
|
||||
|
||||
@ -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
21
src/vm/wren_meta.c
Normal 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
18
src/vm/wren_meta.h
Normal 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
|
||||
@ -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;
|
||||
}
|
||||
|
||||
5
test/meta/eval_existing_scoped_variable.wren
Normal file
5
test/meta/eval_existing_scoped_variable.wren
Normal file
@ -0,0 +1,5 @@
|
||||
var y
|
||||
|
||||
Meta.eval("y = 2")
|
||||
|
||||
IO.print(y) // expect: 2
|
||||
Reference in New Issue
Block a user