mirror of
https://github.com/janet-lang/janet
synced 2025-12-03 15:18:10 +00:00
More work on renaming functions. Change long string syntax to use
backticks. Allow custom masks in fibers for custom error and debug handling.
This commit is contained in:
@@ -105,6 +105,15 @@ Dst dst_array_peek(DstArray *array) {
|
||||
|
||||
/* C Functions */
|
||||
|
||||
static int cfun_new(DstArgs args) {
|
||||
int32_t cap;
|
||||
DstArray *array;
|
||||
dst_fixarity(args, 1);
|
||||
dst_arg_integer(cap, args, 0);
|
||||
array = dst_array(cap);
|
||||
return dst_return(args, dst_wrap_array(array));
|
||||
}
|
||||
|
||||
static int cfun_pop(DstArgs args) {
|
||||
dst_fixarity(args, 1);
|
||||
dst_check(args, 0, DST_ARRAY);
|
||||
@@ -219,6 +228,7 @@ static int cfun_concat(DstArgs args) {
|
||||
}
|
||||
|
||||
static const DstReg cfuns[] = {
|
||||
{"array.new", cfun_new},
|
||||
{"array.pop", cfun_pop},
|
||||
{"array.peek", cfun_peek},
|
||||
{"array.push", cfun_push},
|
||||
|
||||
@@ -157,6 +157,16 @@ int dst_buffer_push_u64(DstBuffer *buffer, uint64_t x) {
|
||||
}
|
||||
|
||||
/* C functions */
|
||||
|
||||
static int cfun_new(DstArgs args) {
|
||||
int32_t cap;
|
||||
DstBuffer *buffer;
|
||||
dst_fixarity(args, 1);
|
||||
dst_arg_integer(cap, args, 0);
|
||||
buffer = dst_buffer(cap);
|
||||
return dst_return(args, dst_wrap_buffer(buffer));
|
||||
}
|
||||
|
||||
static int cfun_u8(DstArgs args) {
|
||||
int32_t i;
|
||||
DstBuffer *buffer;
|
||||
@@ -258,6 +268,7 @@ static int cfun_slice(DstArgs args) {
|
||||
}
|
||||
|
||||
static const DstReg cfuns[] = {
|
||||
{"buffer.new", cfun_new},
|
||||
{"buffer.push-byte", cfun_u8},
|
||||
{"buffer.push-integer", cfun_int},
|
||||
{"buffer.push-string", cfun_chars},
|
||||
|
||||
@@ -202,14 +202,6 @@ int dst_core_struct(DstArgs args) {
|
||||
return dst_return(args, dst_wrap_struct(dst_struct_end(st)));
|
||||
}
|
||||
|
||||
int dst_core_fiber(DstArgs args) {
|
||||
DstFiber *fiber;
|
||||
dst_fixarity(args, 1);
|
||||
dst_check(args, 0, DST_FUNCTION);
|
||||
fiber = dst_fiber(dst_unwrap_function(args.v[0]), 64);
|
||||
return dst_return(args, dst_wrap_fiber(fiber));
|
||||
}
|
||||
|
||||
int dst_core_gensym(DstArgs args) {
|
||||
dst_maxarity(args, 1);
|
||||
if (args.n == 0) {
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include <dst/dst.h>
|
||||
#include <dst/dstopcodes.h>
|
||||
#include "fiber.h"
|
||||
#include "gc.h"
|
||||
|
||||
@@ -257,6 +258,35 @@ void dst_fiber_popframe(DstFiber *fiber) {
|
||||
|
||||
/* CFuns */
|
||||
|
||||
static int cfun_new(DstArgs args) {
|
||||
DstFiber *fiber;
|
||||
dst_minarity(args, 1);
|
||||
dst_maxarity(args, 2);
|
||||
dst_check(args, 0, DST_FUNCTION);
|
||||
fiber = dst_fiber(dst_unwrap_function(args.v[0]), 64);
|
||||
if (args.n == 2) {
|
||||
const uint8_t *flags;
|
||||
int32_t len, i;
|
||||
dst_arg_bytes(flags, len, args, 1);
|
||||
fiber->flags |= DST_FIBER_MASK_ERROR;
|
||||
for (i = 0; i < len; i++) {
|
||||
switch (flags[i]) {
|
||||
default:
|
||||
return dst_throw(args, "invalid flag, expected d or e");
|
||||
case ':':
|
||||
break;
|
||||
case 'd':
|
||||
fiber->flags &= ~DST_FIBER_MASK_DEBUG;
|
||||
break;
|
||||
case 'e':
|
||||
fiber->flags &= ~DST_FIBER_MASK_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst_return(args, dst_wrap_fiber(fiber));
|
||||
}
|
||||
|
||||
static int cfun_status(DstArgs args) {
|
||||
const char *status = "";
|
||||
dst_fixarity(args, 1);
|
||||
@@ -338,6 +368,7 @@ static int cfun_stack(DstArgs args) {
|
||||
}
|
||||
|
||||
static const DstReg cfuns[] = {
|
||||
{"fiber.new", cfun_new},
|
||||
{"fiber.status", cfun_status},
|
||||
{"fiber.stack", cfun_stack},
|
||||
{NULL, NULL}
|
||||
@@ -345,7 +376,21 @@ static const DstReg cfuns[] = {
|
||||
|
||||
/* Module entry point */
|
||||
int dst_lib_fiber(DstArgs args) {
|
||||
static uint32_t yield_asm[] = {
|
||||
DOP_YIELD,
|
||||
DOP_RETURN
|
||||
};
|
||||
static uint32_t resume_asm[] = {
|
||||
DOP_RESUME | (1 << 24),
|
||||
DOP_RETURN
|
||||
};
|
||||
DstTable *env = dst_env_arg(args);
|
||||
dst_env_cfuns(env, cfuns);
|
||||
dst_env_def(env, "fiber.yield",
|
||||
dst_wrap_function(dst_quick_asm(1, 0, 2,
|
||||
yield_asm, sizeof(yield_asm))));
|
||||
dst_env_def(env, "fiber.resume",
|
||||
dst_wrap_function(dst_quick_asm(2, 0, 2,
|
||||
resume_asm, sizeof(resume_asm))));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -235,6 +235,17 @@ void dst_table_merge_struct(DstTable *table, const DstKV *other) {
|
||||
dst_table_mergekv(table, other, dst_struct_capacity(other));
|
||||
}
|
||||
|
||||
/* C Functions */
|
||||
|
||||
static int cfun_new(DstArgs args) {
|
||||
DstTable *t;
|
||||
int32_t cap;
|
||||
dst_fixarity(args, 1);
|
||||
dst_arg_integer(cap, args, 0);
|
||||
t = dst_table(cap);
|
||||
return dst_return(args, dst_wrap_table(t));
|
||||
}
|
||||
|
||||
static int cfun_getproto(DstArgs args) {
|
||||
DstTable *t;
|
||||
dst_fixarity(args, 1);
|
||||
@@ -269,6 +280,7 @@ static int cfun_rawget(DstArgs args) {
|
||||
}
|
||||
|
||||
static const DstReg cfuns[] = {
|
||||
{"table.new", cfun_new},
|
||||
{"table.to-struct", cfun_tostruct},
|
||||
{"table.getproto", cfun_getproto},
|
||||
{"table.setproto", cfun_setproto},
|
||||
|
||||
@@ -255,6 +255,7 @@ int32_t dst_length(Dst x) {
|
||||
default:
|
||||
return 0;
|
||||
case DST_STRING:
|
||||
case DST_SYMBOL:
|
||||
return dst_string_length(dst_unwrap_string(x));
|
||||
case DST_ARRAY:
|
||||
return dst_unwrap_array(x)->count;
|
||||
@@ -275,6 +276,7 @@ Dst dst_getindex(Dst ds, int32_t index) {
|
||||
default:
|
||||
return dst_wrap_nil();
|
||||
case DST_STRING:
|
||||
case DST_SYMBOL:
|
||||
if (index >= dst_string_length(dst_unwrap_string(ds))) return dst_wrap_nil();
|
||||
return dst_wrap_integer(dst_unwrap_string(ds)[index]);
|
||||
case DST_ARRAY:
|
||||
|
||||
@@ -750,7 +750,6 @@ static void *op_lookup[255] = {
|
||||
break;
|
||||
default:
|
||||
fiber->child = NULL;
|
||||
if (nextfiber->flags & DST_FIBER_MASK_RETURN) goto vm_return_root;
|
||||
break;
|
||||
}
|
||||
stack[oparg(1, 0xFF)] = retreg;
|
||||
@@ -844,6 +843,7 @@ static void *op_lookup[255] = {
|
||||
vm_debug:
|
||||
{
|
||||
fiber->status = DST_FIBER_DEBUG;
|
||||
retreg = dst_wrap_nil();
|
||||
goto vm_exit;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user