2013-11-25 16:47:02 +01:00
|
|
|
#ifndef wren_h
|
|
|
|
|
#define wren_h
|
|
|
|
|
|
2015-09-30 04:29:10 +02:00
|
|
|
#include <stdarg.h>
|
2013-11-25 16:47:02 +01:00
|
|
|
#include <stdlib.h>
|
2015-01-17 12:48:27 +01:00
|
|
|
#include <stdbool.h>
|
2013-11-25 16:47:02 +01:00
|
|
|
|
2015-08-07 07:24:15 +02:00
|
|
|
// A single virtual machine for executing Wren code.
|
|
|
|
|
//
|
|
|
|
|
// Wren has no global state, so all state stored by a running interpreter lives
|
|
|
|
|
// here.
|
2013-11-25 16:47:02 +01:00
|
|
|
typedef struct WrenVM WrenVM;
|
|
|
|
|
|
2015-08-07 07:24:15 +02:00
|
|
|
// A handle to a Wren object.
|
|
|
|
|
//
|
|
|
|
|
// This lets code outside of the VM hold a persistent reference to an object.
|
|
|
|
|
// After a value is acquired, and until it is released, this ensures the
|
|
|
|
|
// garbage collector will not reclaim it.
|
|
|
|
|
typedef struct WrenValue WrenValue;
|
|
|
|
|
|
2013-12-14 23:17:16 +01:00
|
|
|
// A generic allocation function that handles all explicit memory management
|
|
|
|
|
// used by Wren. It's used like so:
|
2013-11-25 16:47:02 +01:00
|
|
|
//
|
2015-03-25 15:45:29 +01:00
|
|
|
// - To allocate new memory, [memory] is NULL and [newSize] is the desired
|
|
|
|
|
// size. It should return the allocated memory or NULL on failure.
|
2013-11-25 16:47:02 +01:00
|
|
|
//
|
2015-03-25 15:45:29 +01:00
|
|
|
// - 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.
|
2013-11-25 16:47:02 +01:00
|
|
|
//
|
2015-03-25 15:45:29 +01:00
|
|
|
// - To shrink memory, [memory] and [newSize] are the same as above but it will
|
|
|
|
|
// always return [memory].
|
2013-11-25 16:47:02 +01:00
|
|
|
//
|
2015-03-25 15:45:29 +01:00
|
|
|
// - 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);
|
2013-11-25 16:47:02 +01:00
|
|
|
|
2015-02-06 16:01:15 +01:00
|
|
|
// A function callable from Wren code, but implemented in C.
|
2014-01-01 22:25:24 +01:00
|
|
|
typedef void (*WrenForeignMethodFn)(WrenVM* vm);
|
2013-12-29 19:06:35 +01:00
|
|
|
|
2015-02-06 16:01:15 +01:00
|
|
|
// Loads and returns the source code for the module [name].
|
|
|
|
|
typedef char* (*WrenLoadModuleFn)(WrenVM* vm, const char* name);
|
|
|
|
|
|
2015-03-24 15:58:15 +01:00
|
|
|
// Returns a pointer to a foreign method on [className] in [module] with
|
|
|
|
|
// [signature].
|
|
|
|
|
typedef WrenForeignMethodFn (*WrenBindForeignMethodFn)(WrenVM* vm,
|
|
|
|
|
const char* module,
|
|
|
|
|
const char* className,
|
|
|
|
|
bool isStatic,
|
|
|
|
|
const char* signature);
|
|
|
|
|
|
2015-09-16 08:01:43 +02:00
|
|
|
// Displays a string of text to the user.
|
|
|
|
|
typedef void (*WrenWriteFn)(WrenVM* vm, const char* text);
|
|
|
|
|
|
2015-08-15 21:07:53 +02:00
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
// The callback invoked when the foreign object is created.
|
|
|
|
|
//
|
|
|
|
|
// This must be provided. Inside the body of this, it must call
|
|
|
|
|
// [wrenAllocateForeign] exactly once.
|
|
|
|
|
WrenForeignMethodFn allocate;
|
|
|
|
|
|
|
|
|
|
// The callback invoked when the garbage collector is about to collecto a
|
|
|
|
|
// foreign object's memory.
|
|
|
|
|
//
|
|
|
|
|
// This may be `NULL` if the foreign class does not need to finalize.
|
|
|
|
|
WrenForeignMethodFn finalize;
|
|
|
|
|
} WrenForeignClassMethods;
|
|
|
|
|
|
|
|
|
|
// Returns a pair of pointers to the foreign methods used to allocate and
|
|
|
|
|
// finalize the data for instances of [className] in [module].
|
|
|
|
|
typedef WrenForeignClassMethods (*WrenBindForeignClassFn)(
|
|
|
|
|
WrenVM* vm, const char* module, const char* className);
|
|
|
|
|
|
2013-12-20 16:41:35 +01:00
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
// The callback Wren will use to allocate, reallocate, and deallocate memory.
|
|
|
|
|
//
|
|
|
|
|
// If `NULL`, defaults to a built-in function that uses `realloc` and `free`.
|
|
|
|
|
WrenReallocateFn reallocateFn;
|
|
|
|
|
|
2015-02-06 16:01:15 +01:00
|
|
|
// The callback Wren uses to load a module.
|
|
|
|
|
//
|
|
|
|
|
// Since Wren does not talk directly to the file system, it relies on the
|
|
|
|
|
// embedder to phyisically locate and read the source code for a module. The
|
|
|
|
|
// first time an import appears, Wren will call this and pass in the name of
|
|
|
|
|
// the module being imported. The VM should return the soure code for that
|
|
|
|
|
// module. Memory for the source should be allocated using [reallocateFn] and
|
|
|
|
|
// Wren will take ownership over it.
|
|
|
|
|
//
|
|
|
|
|
// This will only be called once for any given module name. Wren caches the
|
|
|
|
|
// result internally so subsequent imports of the same module will use the
|
|
|
|
|
// previous source and not call this.
|
|
|
|
|
//
|
|
|
|
|
// If a module with the given name could not be found by the embedder, it
|
|
|
|
|
// should return NULL and Wren will report that as a runtime error.
|
|
|
|
|
WrenLoadModuleFn loadModuleFn;
|
|
|
|
|
|
2015-03-24 15:58:15 +01:00
|
|
|
// The callback Wren uses to find a foreign method and bind it to a class.
|
|
|
|
|
//
|
|
|
|
|
// When a foreign method is declared in a class, this will be called with the
|
|
|
|
|
// foreign method's module, class, and signature when the class body is
|
|
|
|
|
// executed. It should return a pointer to the foreign function that will be
|
|
|
|
|
// bound to that method.
|
|
|
|
|
//
|
|
|
|
|
// If the foreign function could not be found, this should return NULL and
|
|
|
|
|
// Wren will report it as runtime error.
|
|
|
|
|
WrenBindForeignMethodFn bindForeignMethodFn;
|
|
|
|
|
|
2015-08-15 21:07:53 +02:00
|
|
|
// The callback Wren uses to find a foreign class and get its foreign methods.
|
|
|
|
|
//
|
|
|
|
|
// When a foreign class is declared, this will be called with the class's
|
|
|
|
|
// module and name when the class body is executed. It should return the
|
|
|
|
|
// foreign functions uses to allocate and (optionally) finalize the bytes
|
|
|
|
|
// stored in the foreign object when an instance is created.
|
|
|
|
|
WrenBindForeignClassFn bindForeignClassFn;
|
|
|
|
|
|
2015-09-16 08:01:43 +02:00
|
|
|
// The callback Wren uses to display text when `System.print()` or the other
|
|
|
|
|
// related functions are called.
|
|
|
|
|
//
|
|
|
|
|
// If this is `NULL`, Wren discards any printed text.
|
|
|
|
|
WrenWriteFn writeFn;
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
|
2013-12-20 16:41:35 +01:00
|
|
|
// The number of bytes Wren will allocate before triggering the first garbage
|
|
|
|
|
// collection.
|
|
|
|
|
//
|
|
|
|
|
// If zero, defaults to 10MB.
|
|
|
|
|
size_t initialHeapSize;
|
|
|
|
|
|
|
|
|
|
// After a collection occurs, the threshold for the next collection is
|
|
|
|
|
// determined based on the number of bytes remaining in use. This allows Wren
|
|
|
|
|
// to shrink its memory usage automatically after reclaiming a large amount
|
|
|
|
|
// of memory.
|
|
|
|
|
//
|
|
|
|
|
// This can be used to ensure that the heap does not get too small, which can
|
|
|
|
|
// in turn lead to a large number of collections afterwards as the heap grows
|
|
|
|
|
// back to a usable size.
|
|
|
|
|
//
|
|
|
|
|
// If zero, defaults to 1MB.
|
|
|
|
|
size_t minHeapSize;
|
|
|
|
|
|
|
|
|
|
// Wren will grow (and shrink) the heap automatically as the number of bytes
|
|
|
|
|
// remaining in use after a collection changes. This number determines the
|
|
|
|
|
// amount of additional memory Wren will use after a collection, as a
|
|
|
|
|
// percentage of the current heap size.
|
|
|
|
|
//
|
|
|
|
|
// For example, say that this is 50. After a garbage collection, Wren there
|
|
|
|
|
// are 400 bytes of memory still in use. That means the next collection will
|
|
|
|
|
// be triggered after a total of 600 bytes are allocated (including the 400
|
|
|
|
|
// already in use.
|
|
|
|
|
//
|
|
|
|
|
// Setting this to a smaller number wastes less memory, but triggers more
|
|
|
|
|
// frequent garbage collections.
|
|
|
|
|
//
|
|
|
|
|
// If zero, defaults to 50.
|
|
|
|
|
int heapGrowthPercent;
|
|
|
|
|
} WrenConfiguration;
|
|
|
|
|
|
2014-02-03 15:51:44 +01:00
|
|
|
typedef enum {
|
|
|
|
|
WREN_RESULT_SUCCESS,
|
|
|
|
|
WREN_RESULT_COMPILE_ERROR,
|
|
|
|
|
WREN_RESULT_RUNTIME_ERROR
|
|
|
|
|
} WrenInterpretResult;
|
|
|
|
|
|
2015-08-15 21:07:53 +02:00
|
|
|
// Initializes [configuration] with all of its default values.
|
|
|
|
|
//
|
|
|
|
|
// Call this before setting the particular fields you care about.
|
|
|
|
|
void wrenInitConfiguration(WrenConfiguration* configuration);
|
|
|
|
|
|
2013-12-20 16:41:35 +01:00
|
|
|
// Creates a new Wren virtual machine using the given [configuration]. Wren
|
|
|
|
|
// will copy the configuration data, so the argument passed to this can be
|
|
|
|
|
// freed after calling this. If [configuration] is `NULL`, uses a default
|
|
|
|
|
// configuration.
|
|
|
|
|
WrenVM* wrenNewVM(WrenConfiguration* configuration);
|
2013-11-25 16:47:02 +01:00
|
|
|
|
|
|
|
|
// Disposes of all resources is use by [vm], which was previously created by a
|
|
|
|
|
// call to [wrenNewVM].
|
|
|
|
|
void wrenFreeVM(WrenVM* vm);
|
|
|
|
|
|
2015-09-01 06:56:21 +02:00
|
|
|
// Immediately run the garbage collector to free unused memory.
|
|
|
|
|
void wrenCollectGarbage(WrenVM* vm);
|
|
|
|
|
|
2014-02-03 15:51:44 +01:00
|
|
|
// Runs [source], a string of Wren source code in a new fiber in [vm].
|
2015-10-16 03:17:42 +02:00
|
|
|
WrenInterpretResult wrenInterpret(WrenVM* vm, const char* source);
|
2013-11-25 16:47:02 +01:00
|
|
|
|
2015-02-28 22:31:15 +01:00
|
|
|
// Creates a handle that can be used to invoke a method with [signature] on the
|
|
|
|
|
// object in [module] currently stored in top-level [variable].
|
|
|
|
|
//
|
|
|
|
|
// This handle can be used repeatedly to directly invoke that method from C
|
|
|
|
|
// code using [wrenCall].
|
|
|
|
|
//
|
2015-08-31 16:57:48 +02:00
|
|
|
// When done with this handle, it must be released using [wrenReleaseValue].
|
|
|
|
|
WrenValue* wrenGetMethod(WrenVM* vm, const char* module, const char* variable,
|
|
|
|
|
const char* signature);
|
2015-02-28 22:31:15 +01:00
|
|
|
|
|
|
|
|
// Calls [method], passing in a series of arguments whose types must match the
|
|
|
|
|
// specifed [argTypes]. This is a string where each character identifies the
|
2015-04-18 04:13:36 +02:00
|
|
|
// type of a single argument, in order. The allowed types are:
|
2015-02-28 22:31:15 +01:00
|
|
|
//
|
|
|
|
|
// - "b" - A C `int` converted to a Wren Bool.
|
|
|
|
|
// - "d" - A C `double` converted to a Wren Num.
|
|
|
|
|
// - "i" - A C `int` converted to a Wren Num.
|
|
|
|
|
// - "s" - A C null-terminated `const char*` converted to a Wren String. Wren
|
|
|
|
|
// will allocate its own string and copy the characters from this, so
|
|
|
|
|
// you don't have to worry about the lifetime of the string you pass to
|
|
|
|
|
// Wren.
|
2015-09-24 17:02:31 +02:00
|
|
|
// - "a" - An array of bytes converted to a Wren String. This requires two
|
|
|
|
|
// consecutive arguments in the argument list: `const char*` pointing
|
|
|
|
|
// to the array of bytes, followed by an `int` defining the length of
|
|
|
|
|
// the array. This is used when the passed string may contain null
|
|
|
|
|
// bytes, or just to avoid the implicit `strlen()` call of "s" if you
|
|
|
|
|
// happen to already know the length.
|
2015-08-07 16:57:23 +02:00
|
|
|
// - "v" - A previously acquired WrenValue*. Passing this in does not implicitly
|
2015-09-24 17:02:31 +02:00
|
|
|
// release the value. If the passed argument is NULL, this becomes a
|
|
|
|
|
// Wren NULL.
|
2015-08-31 16:57:48 +02:00
|
|
|
//
|
2015-09-30 04:29:10 +02:00
|
|
|
// [method] must have been created by a call to [wrenGetMethod]. If
|
|
|
|
|
// [returnValue] is not `NULL`, the return value of the method will be stored
|
|
|
|
|
// in a new [WrenValue] that [returnValue] will point to. Don't forget to
|
|
|
|
|
// release it, when done with it.
|
|
|
|
|
WrenInterpretResult wrenCall(WrenVM* vm, WrenValue* method,
|
|
|
|
|
WrenValue** returnValue,
|
2015-08-31 16:57:48 +02:00
|
|
|
const char* argTypes, ...);
|
2015-02-28 22:31:15 +01:00
|
|
|
|
2015-09-30 04:29:10 +02:00
|
|
|
WrenInterpretResult wrenCallVarArgs(WrenVM* vm, WrenValue* method,
|
|
|
|
|
WrenValue** returnValue,
|
|
|
|
|
const char* argTypes, va_list args);
|
|
|
|
|
|
2015-08-07 07:24:15 +02:00
|
|
|
// Releases the reference stored in [value]. After calling this, [value] can no
|
|
|
|
|
// longer be used.
|
|
|
|
|
void wrenReleaseValue(WrenVM* vm, WrenValue* value);
|
|
|
|
|
|
2015-08-15 21:07:53 +02:00
|
|
|
// This must be called once inside a foreign class's allocator function.
|
|
|
|
|
//
|
|
|
|
|
// It tells Wren how many bytes of raw data need to be stored in the foreign
|
|
|
|
|
// object and creates the new object with that size. It returns a pointer to
|
|
|
|
|
// the foreign object's data.
|
|
|
|
|
void* wrenAllocateForeign(WrenVM* vm, size_t size);
|
|
|
|
|
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
// Returns the number of slots available to the current foreign method.
|
|
|
|
|
int wrenGetSlotCount(WrenVM* vm);
|
2015-08-15 21:07:53 +02:00
|
|
|
|
2015-12-16 22:00:13 +01:00
|
|
|
// TODO: Update docs.
|
|
|
|
|
|
2015-01-22 16:05:55 +01:00
|
|
|
// The following functions read one of the arguments passed to a foreign call.
|
|
|
|
|
// They may only be called while within a function provided to
|
|
|
|
|
// [wrenDefineMethod] or [wrenDefineStaticMethod] that Wren has invoked.
|
|
|
|
|
//
|
|
|
|
|
// They retreive the argument at a given index which ranges from 0 to the number
|
|
|
|
|
// of parameters the method expects. The zeroth parameter is used for the
|
|
|
|
|
// receiver of the method. For example, given a foreign method "foo" on String
|
|
|
|
|
// invoked like:
|
|
|
|
|
//
|
|
|
|
|
// "receiver".foo("one", "two", "three")
|
|
|
|
|
//
|
|
|
|
|
// The foreign function will be able to access the arguments like so:
|
|
|
|
|
//
|
|
|
|
|
// 0: "receiver"
|
|
|
|
|
// 1: "one"
|
|
|
|
|
// 2: "two"
|
|
|
|
|
// 3: "three"
|
|
|
|
|
//
|
|
|
|
|
// It is an error to pass an invalid argument index.
|
|
|
|
|
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
// Reads a boolean value from [slot].
|
|
|
|
|
//
|
|
|
|
|
// It is an error to call this if the slot does not contain a boolean value.
|
|
|
|
|
bool wrenGetSlotBool(WrenVM* vm, int slot);
|
2015-01-20 23:00:03 +01:00
|
|
|
|
2015-12-16 22:00:13 +01:00
|
|
|
// Reads a byte array from [slot].
|
|
|
|
|
//
|
|
|
|
|
// The memory for the returned string is owned by Wren. You can inspect it
|
|
|
|
|
// while in your foreign method, but cannot keep a pointer to it after the
|
|
|
|
|
// function returns, since the garbage collector may reclaim it.
|
|
|
|
|
//
|
|
|
|
|
// Returns a pointer to the first byte of the array and fill [length] with the
|
|
|
|
|
// number of bytes in the array.
|
|
|
|
|
//
|
|
|
|
|
// It is an error to call this if the slot does not contain a string.
|
|
|
|
|
const char* wrenGetSlotBytes(WrenVM* vm, int slot, int* length);
|
|
|
|
|
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
// Reads a number from [slot].
|
|
|
|
|
//
|
|
|
|
|
// It is an error to call this if the slot does not contain a number.
|
|
|
|
|
double wrenGetSlotDouble(WrenVM* vm, int slot);
|
2013-12-29 19:06:35 +01:00
|
|
|
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
// Reads a foreign object from [slot] and returns a pointer to the foreign data
|
|
|
|
|
// stored with it.
|
|
|
|
|
//
|
|
|
|
|
// It is an error to call this if the slot does not contain an instance of a
|
|
|
|
|
// foreign class.
|
|
|
|
|
void* wrenGetSlotForeign(WrenVM* vm, int slot);
|
2015-08-15 21:07:53 +02:00
|
|
|
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
// Reads a string from [slot].
|
2014-02-04 17:44:59 +01:00
|
|
|
//
|
|
|
|
|
// The memory for the returned string is owned by Wren. You can inspect it
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
// while in your foreign method, but cannot keep a pointer to it after the
|
2014-02-04 17:44:59 +01:00
|
|
|
// function returns, since the garbage collector may reclaim it.
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
//
|
|
|
|
|
// It is an error to call this if the slot does not contain a string.
|
|
|
|
|
const char* wrenGetSlotString(WrenVM* vm, int slot);
|
2014-02-04 17:44:59 +01:00
|
|
|
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
// Creates a handle for the value stored in [slot].
|
2015-08-07 07:24:15 +02:00
|
|
|
//
|
|
|
|
|
// This will prevent the object that is referred to from being garbage collected
|
|
|
|
|
// until the handle is released by calling [wrenReleaseValue()].
|
refactor: rename wrenGetArgument* to wrenGetSlot* and add IS_LIST macro for slot-based API
Migrate the foreign method argument access API from an argument-oriented naming
scheme to a slot-based one, renaming wrenGetArgumentCount to wrenGetSlotCount,
wrenGetArgumentBool to wrenGetSlotBool, and all other argument accessors
accordingly. Update every call site across the VM, modules (io, timer, meta,
random), and test files (benchmark, foreign_class) to use the new names. The
slot getters now assert the correct type at runtime rather than silently
returning defaults, shifting safety responsibility to the caller for
performance. Also add the IS_LIST type-checking macro to wren_value.h alongside
the existing IS_* macros.
2015-12-16 19:22:42 +01:00
|
|
|
WrenValue* wrenGetSlotValue(WrenVM* vm, int slot);
|
2015-08-07 07:24:15 +02:00
|
|
|
|
2015-01-22 16:05:55 +01:00
|
|
|
// The following functions provide the return value for a foreign method back
|
|
|
|
|
// to Wren. Like above, they may only be called during a foreign call invoked
|
|
|
|
|
// by Wren.
|
|
|
|
|
//
|
|
|
|
|
// If none of these is called by the time the foreign function returns, the
|
|
|
|
|
// method implicitly returns `null`. Within a given foreign call, you may only
|
|
|
|
|
// call one of these once. It is an error to access any of the foreign calls
|
|
|
|
|
// arguments after one of these has been called.
|
|
|
|
|
|
2015-12-16 22:00:13 +01:00
|
|
|
// Stores the boolean [value] in [slot].
|
|
|
|
|
void wrenSetSlotBool(WrenVM* vm, int slot, bool value);
|
2013-12-29 19:06:35 +01:00
|
|
|
|
2015-12-16 22:00:13 +01:00
|
|
|
// Stores the array [length] of [bytes] in [slot].
|
2014-02-05 15:30:20 +01:00
|
|
|
//
|
2015-12-16 22:00:13 +01:00
|
|
|
// The bytes are copied to a new string within Wren's heap, so you can free
|
|
|
|
|
// memory used by them after this is called.
|
|
|
|
|
void wrenSetSlotBytes(WrenVM* vm, int slot, const char* bytes, int length);
|
|
|
|
|
|
|
|
|
|
// Stores the numeric [value] in [slot].
|
|
|
|
|
void wrenSetSlotDouble(WrenVM* vm, int slot, double value);
|
|
|
|
|
|
|
|
|
|
// Stores null in [slot].
|
|
|
|
|
void wrenSetSlotNull(WrenVM* vm, int slot);
|
|
|
|
|
|
|
|
|
|
// Stores the string [text] in [slot].
|
2015-09-24 17:02:31 +02:00
|
|
|
//
|
2015-12-16 22:00:13 +01:00
|
|
|
// The [text] is copied to a new string within Wren's heap, so you can free
|
|
|
|
|
// memory used by it after this is called. The length is calculated using
|
|
|
|
|
// [strlen()]. If the string may contain any null bytes in the middle, then you
|
|
|
|
|
// should use [wrenSetSlotBytes()] instead.
|
|
|
|
|
void wrenSetSlotString(WrenVM* vm, int slot, const char* text);
|
2014-02-05 15:30:20 +01:00
|
|
|
|
2015-12-16 22:00:13 +01:00
|
|
|
// Stores the value captured in [value] in [slot].
|
2015-08-07 07:24:15 +02:00
|
|
|
//
|
2015-12-16 22:00:13 +01:00
|
|
|
// This does not release the handle for the value.
|
|
|
|
|
void wrenSetSlotValue(WrenVM* vm, int slot, WrenValue* value);
|
2015-08-07 07:24:15 +02:00
|
|
|
|
2013-11-25 16:47:02 +01:00
|
|
|
#endif
|