replace vm_type comparison with .is_xxx

This commit is contained in:
ValKmjolnir 2023-11-23 00:20:52 +08:00
parent be84388f5b
commit 01ceaf7e66
16 changed files with 160 additions and 146 deletions

View File

@ -66,7 +66,7 @@ void ghost_for_test_gc_marker(void* ptr, std::vector<var>* bfs_queue) {
}
var create_new_ghost(var* args, usize size, gc* ngc) {
var res = ngc->alloc(vm_type::vm_obj);
var res = ngc->alloc(vm_type::vm_ghost);
res.ghost().set(
ghost_for_test,
ghost_for_test_destructor,

View File

@ -21,7 +21,7 @@ var nas_vec3(var* args, usize size, gc* ngc) {
}
var nas_vec2_add(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -34,7 +34,7 @@ var nas_vec2_add(var* args, usize size, gc* ngc) {
}
var nas_vec2_sub(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -47,7 +47,7 @@ var nas_vec2_sub(var* args, usize size, gc* ngc) {
}
var nas_vec2_mult(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -60,7 +60,7 @@ var nas_vec2_mult(var* args, usize size, gc* ngc) {
}
var nas_vec2_div(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -73,7 +73,7 @@ var nas_vec2_div(var* args, usize size, gc* ngc) {
}
var nas_vec2_neg(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=2)
@ -85,7 +85,7 @@ var nas_vec2_neg(var* args, usize size, gc* ngc) {
}
var nas_vec2_norm(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=2)
@ -100,7 +100,7 @@ var nas_vec2_norm(var* args, usize size, gc* ngc) {
}
var nas_vec2_len(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=2)
@ -111,7 +111,7 @@ var nas_vec2_len(var* args, usize size, gc* ngc) {
}
var nas_vec2_dot(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -121,7 +121,7 @@ var nas_vec2_dot(var* args, usize size, gc* ngc) {
}
var nas_vec3_add(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -135,7 +135,7 @@ var nas_vec3_add(var* args, usize size, gc* ngc) {
}
var nas_vec3_sub(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -149,7 +149,7 @@ var nas_vec3_sub(var* args, usize size, gc* ngc) {
}
var nas_vec3_mult(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -163,7 +163,7 @@ var nas_vec3_mult(var* args, usize size, gc* ngc) {
}
var nas_vec3_div(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;
@ -177,7 +177,7 @@ var nas_vec3_div(var* args, usize size, gc* ngc) {
}
var nas_vec3_neg(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=3)
@ -190,7 +190,7 @@ var nas_vec3_neg(var* args, usize size, gc* ngc) {
}
var nas_vec3_norm(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=3)
@ -207,7 +207,7 @@ var nas_vec3_norm(var* args, usize size, gc* ngc) {
}
var nas_vec3_len(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=3)
@ -219,7 +219,7 @@ var nas_vec3_len(var* args, usize size, gc* ngc) {
}
var nas_rotate_x(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=3)
@ -233,7 +233,7 @@ var nas_rotate_x(var* args, usize size, gc* ngc) {
}
var nas_rotate_y(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=3)
@ -247,7 +247,7 @@ var nas_rotate_y(var* args, usize size, gc* ngc) {
}
var nas_rotate_z(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec)
if (!args[0].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
if (v0.size()!=3)
@ -261,7 +261,7 @@ var nas_rotate_z(var* args, usize size, gc* ngc) {
}
var nas_vec3_dot(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_vec || args[1].type!=vm_type::vm_vec)
if (!args[0].is_vec() || !args[1].is_vec())
return nil;
auto& v0 = args[0].vec().elems;
auto& v1 = args[1].vec().elems;

View File

@ -33,14 +33,14 @@ static WSAmanager win;
namespace nasal {
var nas_socket(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num || args[1].type!=vm_type::vm_num || args[2].type!=vm_type::vm_num)
if (!args[0].is_num() || !args[1].is_num() || !args[2].is_num())
return nas_err("socket", "\"af\", \"type\", \"protocol\" should be number");
int sd = socket(args[0].num(), args[1].num(), args[2].num());
return var::num(static_cast<double>(sd));
}
var nas_closesocket(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("closesocket", "\"sd\" should be number");
#ifdef _WIN32
return var::num(static_cast<double>(closesocket(args[0].num())));
@ -50,19 +50,19 @@ var nas_closesocket(var* args, usize size, gc* ngc) {
}
var nas_shutdown(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("shutdown", "\"sd\" must be a number");
if (args[1].type!=vm_type::vm_num)
if (!args[1].is_num())
return nas_err("shutdown", "\"how\" must be a number");
return var::num(static_cast<double>(shutdown(args[0].num(), args[1].num())));
}
var nas_bind(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("bind", "\"sd\" muse be a number");
if (args[1].type!=vm_type::vm_str)
if (!args[1].is_str())
return nas_err("bind", "\"ip\" should be a string including an ip with correct format");
if (args[2].type!=vm_type::vm_num)
if (!args[2].is_num())
return nas_err("bind", "\"port\" must be a number");
sockaddr_in server;
memset(&server, 0, sizeof(sockaddr_in));
@ -77,19 +77,19 @@ var nas_bind(var* args, usize size, gc* ngc) {
}
var nas_listen(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("listen", "\"sd\" must be a number");
if (args[1].type!=vm_type::vm_num)
if (!args[1].is_num())
return nas_err("listen", "\"backlog\" must be a number");
return var::num(static_cast<double>(listen(args[0].num(), args[1].num())));
}
var nas_connect(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("connect", "\"sd\" must be a number");
if (args[1].type!=vm_type::vm_str)
if (!args[1].is_str())
return nas_err("connect", "\"hostname\" must be a string");
if (args[2].type!=vm_type::vm_num)
if (!args[2].is_num())
return nas_err("connect", "\"port\" must be a number");
sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in));
@ -105,7 +105,7 @@ var nas_connect(var* args, usize size, gc* ngc) {
}
var nas_accept(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("accept", "\"sd\" must be a number");
sockaddr_in client;
int socklen = sizeof(sockaddr_in);
@ -131,11 +131,11 @@ var nas_accept(var* args, usize size, gc* ngc) {
}
var nas_send(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("send", "\"sd\" must be a number");
if (args[1].type!=vm_type::vm_str)
if (!args[1].is_str())
return nas_err("send", "\"buff\" must be a string");
if (args[2].type!=vm_type::vm_num)
if (!args[2].is_num())
return nas_err("send", "\"flags\" muse be a number");
return var::num(static_cast<double>(send(
args[0].num(),
@ -146,15 +146,15 @@ var nas_send(var* args, usize size, gc* ngc) {
}
var nas_sendto(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("sendto", "\"sd\" must be a number");
if (args[1].type!=vm_type::vm_str)
if (!args[1].is_str())
return nas_err("sendto", "\"hostname\" must be a string");
if (args[2].type!=vm_type::vm_num)
if (!args[2].is_num())
return nas_err("sendto", "\"port\" must be a number");
if (args[3].type!=vm_type::vm_str)
if (!args[3].is_str())
return nas_err("sendto", "\"buff\" must be a string");
if (args[4].type!=vm_type::vm_num)
if (!args[4].is_num())
return nas_err("sendto", "\"flags\" must be a number");
sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in));
@ -173,13 +173,13 @@ var nas_sendto(var* args, usize size, gc* ngc) {
}
var nas_recv(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("recv", "\"sd\" must be a number");
if (args[1].type!=vm_type::vm_num)
if (!args[1].is_num())
return nas_err("recv", "\"len\" must be a number");
if (args[1].num()<=0 || args[1].num()>16*1024*1024)
return nas_err("recv", "\"len\" out of range");
if (args[2].type!=vm_type::vm_num)
if (!args[2].is_num())
return nas_err("recv", "\"flags\" muse be a number");
var res = ngc->temp = ngc->alloc(vm_type::vm_hash);
auto& hash = res.hash().elems;
@ -194,13 +194,13 @@ var nas_recv(var* args, usize size, gc* ngc) {
}
var nas_recvfrom(var* args, usize size, gc* ngc) {
if (args[0].type!=vm_type::vm_num)
if (!args[0].is_num())
return nas_err("recvfrom", "\"sd\" must be a number");
if (args[1].type!=vm_type::vm_num)
if (!args[1].is_num())
return nas_err("recvfrom", "\"len\" must be a number");
if (args[1].num()<=0 || args[1].num()>16*1024*1024)
return nas_err("recvfrom", "\"len\" out of range");
if (args[2].type!=vm_type::vm_num)
if (!args[2].is_num())
return nas_err("recvfrom", "\"flags\" muse be a number");
sockaddr_in addr;
int socklen = sizeof(sockaddr_in);

View File

@ -48,11 +48,11 @@ var builtin_fld(context* ctx, gc* ngc) {
auto str = local[1];
auto startbit = local[2];
auto length = local[3];
if (str.type!=vm_type::vm_str || str.val.gcobj->unmutable) {
if (!str.is_str() || str.val.gcobj->unmutable) {
return nas_err("bits::fld", "\"str\" must be mutable string");
}
if (startbit.type!=vm_type::vm_num || length.type!=vm_type::vm_num) {
return nas_err("bits::fld", "\"startbit\",\"len\" must be number");
if (!startbit.is_num() || !length.is_num()) {
return nas_err("bits::fld", "\"startbit\", \"len\" must be number");
}
u32 bit = static_cast<u32>(startbit.num());
u32 len = static_cast<u32>(length.num());
@ -78,10 +78,10 @@ var builtin_sfld(context* ctx, gc* ngc) {
auto str = local[1];
auto startbit = local[2];
auto length = local[3];
if (str.type!=vm_type::vm_str || str.val.gcobj->unmutable) {
if (!str.is_str() || str.val.gcobj->unmutable) {
return nas_err("bits::sfld", "\"str\" must be mutable string");
}
if (startbit.type!=vm_type::vm_num || length.type!=vm_type::vm_num) {
if (!startbit.is_num() || !length.is_num()) {
return nas_err("bits::sfld", "\"startbit\",\"len\" must be number");
}
u32 bit = static_cast<u32>(startbit.num());
@ -112,12 +112,10 @@ var builtin_setfld(context* ctx, gc* ngc) {
auto startbit = local[2];
auto length = local[3];
auto value = local[4];
if (str.type!=vm_type::vm_str || str.val.gcobj->unmutable) {
if (!str.is_str() || str.val.gcobj->unmutable) {
return nas_err("bits::setfld", "\"str\" must be mutable string");
}
if (startbit.type!=vm_type::vm_num ||
length.type!=vm_type::vm_num ||
value.type!=vm_type::vm_num) {
if (!startbit.is_num() || !length.is_num() || !value.is_num()) {
return nas_err("bits::setfld",
"\"startbit\", \"len\", \"val\" must be number"
);
@ -141,7 +139,7 @@ var builtin_setfld(context* ctx, gc* ngc) {
var builtin_buf(context* ctx, gc* ngc) {
var length = ctx->localr[1];
if (length.type!=vm_type::vm_num || length.num()<=0) {
if (!length.is_num() || length.num()<=0) {
return nas_err("bits::buf", "\"len\" must be number greater than 0");
}
var str = ngc->alloc(vm_type::vm_str);

View File

@ -18,7 +18,7 @@ var builtin_cocreate(context* ctx, gc* ngc) {
// +-------------+
// ```
auto coroutine_function = ctx->localr[1];
if (coroutine_function.type!=vm_type::vm_func) {
if (!coroutine_function.is_func()) {
return nas_err(
"coroutine::create",
"must use a function to create coroutine"
@ -69,7 +69,7 @@ var builtin_coresume(context* ctx, gc* ngc) {
auto main_local_frame = ctx->localr;
auto coroutine_object = main_local_frame[1];
// return nil if is not a coroutine object or coroutine exited
if (coroutine_object.type!=vm_type::vm_co ||
if (!coroutine_object.is_coroutine() ||
coroutine_object.co().status==nas_co::status::dead) {
return nil;
}
@ -80,7 +80,7 @@ var builtin_coresume(context* ctx, gc* ngc) {
// fetch coroutine's stack top and return
// then coroutine's stack top will catch this return value
// so the coroutine's stack top in fact is not changed
if (ngc->running_context->top[0].type==vm_type::vm_ret) {
if (ngc->running_context->top[0].is_ret()) {
// when first calling this coroutine, the stack top must be vm_ret
return ngc->running_context->top[0];
}
@ -114,7 +114,7 @@ var builtin_coyield(context* ctx, gc* ngc) {
var builtin_costatus(context* ctx, gc* ngc) {
auto coroutine_object = ctx->localr[1];
if (coroutine_object.type!=vm_type::vm_co) {
if (!coroutine_object.is_coroutine()) {
return ngc->newstr("error");
}
switch(coroutine_object.co().status) {

View File

@ -15,7 +15,7 @@ void dynamic_library_destructor(void* pointer) {
var builtin_dlopen(context* ctx, gc* ngc) {
auto dlname = ctx->localr[1];
if (dlname.type!=vm_type::vm_str) {
if (!dlname.is_str()) {
return nas_err("dylib::dlopen", "\"libname\" must be string");
}
@ -43,7 +43,7 @@ var builtin_dlopen(context* ctx, gc* ngc) {
);
}
auto return_hash = ngc->temp = ngc->alloc(vm_type::vm_hash);
auto library_object = ngc->alloc(vm_type::vm_obj);
auto library_object = ngc->alloc(vm_type::vm_ghost);
library_object.ghost().set(
dynamic_library_type_name,
dynamic_library_destructor,
@ -73,7 +73,7 @@ var builtin_dlopen(context* ctx, gc* ngc) {
}
for(u32 i = 0; table[i].name; ++i) {
auto function_pointer = reinterpret_cast<void*>(table[i].fd);
auto function_object = ngc->alloc(vm_type::vm_obj);
auto function_object = ngc->alloc(vm_type::vm_ghost);
function_object.ghost().set(
function_address_type_name,
nullptr,

View File

@ -8,7 +8,7 @@ var builtin_logprint(context* ctx, gc* ngc) {
auto local = ctx->localr;
auto level = local[1];
auto elems = local[2];
if (elems.type!=vm_type::vm_vec) {
if (!elems.is_vec()) {
return nas_err("fg_env::logprint", "received argument is not vector.");
}
std::ofstream out("fgfs.log", std::ios::app);

View File

@ -58,7 +58,7 @@ var builtin_open(context* ctx, gc* ngc) {
if (!file_descriptor) {
return nas_err("io::open", "failed to open file <" + name.str() + ">");
}
var return_object = ngc->alloc(vm_type::vm_obj);
var return_object = ngc->alloc(vm_type::vm_ghost);
return_object.ghost().set(
file_type_name, filehandle_destructor, nullptr, file_descriptor
);
@ -205,19 +205,19 @@ var builtin_eof(context* ctx, gc* ngc) {
}
var builtin_stdin(context* ctx, gc* ngc) {
auto file_descriptor = ngc->alloc(vm_type::vm_obj);
auto file_descriptor = ngc->alloc(vm_type::vm_ghost);
file_descriptor.ghost().set(file_type_name, nullptr, nullptr, stdin);
return file_descriptor;
}
var builtin_stdout(context* ctx, gc* ngc) {
auto file_descriptor = ngc->alloc(vm_type::vm_obj);
auto file_descriptor = ngc->alloc(vm_type::vm_ghost);
file_descriptor.ghost().set(file_type_name, nullptr, nullptr, stdout);
return file_descriptor;
}
var builtin_stderr(context* ctx, gc* ngc) {
auto file_descriptor = ngc->alloc(vm_type::vm_obj);
auto file_descriptor = ngc->alloc(vm_type::vm_ghost);
file_descriptor.ghost().set(file_type_name, nullptr, nullptr, stderr);
return file_descriptor;
}

View File

@ -13,37 +13,37 @@ var builtin_pow(context* ctx, gc* ngc) {
var builtin_sin(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
return var::num(val.type==vm_type::vm_num? sin(val.num()):std::nan(""));
return var::num(val.is_num()? sin(val.num()):std::nan(""));
}
var builtin_cos(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
return var::num(val.type==vm_type::vm_num? cos(val.num()):std::nan(""));
return var::num(val.is_num()? cos(val.num()):std::nan(""));
}
var builtin_tan(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
return var::num(val.type==vm_type::vm_num? tan(val.num()):std::nan(""));
return var::num(val.is_num()? tan(val.num()):std::nan(""));
}
var builtin_exp(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
return var::num(val.type==vm_type::vm_num? exp(val.num()):std::nan(""));
return var::num(val.is_num()? exp(val.num()):std::nan(""));
}
var builtin_lg(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
return var::num(val.type==vm_type::vm_num? log(val.num())/log(10.0):std::nan(""));
return var::num(val.is_num()? log(val.num())/log(10.0):std::nan(""));
}
var builtin_ln(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
return var::num(val.type==vm_type::vm_num? log(val.num()):std::nan(""));
return var::num(val.is_num()? log(val.num()):std::nan(""));
}
var builtin_sqrt(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
return var::num(val.type==vm_type::vm_num? sqrt(val.num()):std::nan(""));
return var::num(val.is_num()? sqrt(val.num()):std::nan(""));
}
var builtin_atan2(context* ctx, gc* ngc) {
@ -57,7 +57,7 @@ var builtin_atan2(context* ctx, gc* ngc) {
var builtin_isnan(context* ctx, gc* ngc) {
auto x = ctx->localr[1];
return (x.type==vm_type::vm_num && std::isnan(x.num()))? one:zero;
return (x.is_num() && std::isnan(x.num()))? one:zero;
}
nasal_builtin_table math_lib_native[] = {

View File

@ -122,7 +122,7 @@ var builtin_rand(context* ctx, gc* ngc) {
if (val.type!=vm_type::vm_num && val.type!=vm_type::vm_nil) {
return nas_err("rand", "\"seed\" must be nil or number");
}
if (val.type==vm_type::vm_num) {
if (val.is_num()) {
srand(static_cast<u32>(val.num()));
return nil;
}
@ -164,7 +164,7 @@ var builtin_ceil(context* ctx, gc* ngc) {
var builtin_num(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
if (val.type==vm_type::vm_num) {
if (val.is_num()) {
return val;
}
if (val.type!=vm_type::vm_str) {
@ -251,7 +251,7 @@ var builtin_keys(context* ctx, gc* ngc) {
// avoid being sweeped
auto res = ngc->temp = ngc->alloc(vm_type::vm_vec);
auto& vec = res.vec().elems;
if (hash.type==vm_type::vm_hash) {
if (hash.is_hash()) {
for(const auto& iter : hash.hash().elems) {
vec.push_back(ngc->newstr(iter.first));
}
@ -288,7 +288,7 @@ var builtin_type(context* ctx, gc* ngc) {
case vm_type::vm_vec: return ngc->newstr("vec");
case vm_type::vm_hash: return ngc->newstr("hash");
case vm_type::vm_func: return ngc->newstr("func");
case vm_type::vm_obj: return ngc->newstr("obj");
case vm_type::vm_ghost: return ngc->newstr("ghost");
case vm_type::vm_co: return ngc->newstr("coroutine");
case vm_type::vm_map: return ngc->newstr("namespace");
}
@ -415,7 +415,7 @@ var builtin_values(context* ctx, gc* ngc) {
}
auto vec = ngc->alloc(vm_type::vm_vec);
auto& v = vec.vec().elems;
if (hash.type==vm_type::vm_hash) {
if (hash.is_hash()) {
for(auto& i : hash.hash().elems) {
v.push_back(i.second);
}
@ -590,8 +590,8 @@ var builtin_gcextend(context* ctx, gc* ngc) {
ngc->extend(vm_type::vm_func);
} else if (s=="upval") {
ngc->extend(vm_type::vm_upval);
} else if (s=="obj") {
ngc->extend(vm_type::vm_obj);
} else if (s=="ghost") {
ngc->extend(vm_type::vm_ghost);
} else if (s=="co") {
ngc->extend(vm_type::vm_co);
}
@ -634,7 +634,7 @@ var builtin_logtime(context* ctx, gc* ngc) {
var builtin_ghosttype(context* ctx, gc* ngc) {
auto arg = ctx->localr[1];
if (arg.type!=vm_type::vm_obj) {
if (!arg.is_ghost()) {
return nas_err("ghosttype", "this is not a ghost object.");
}
const auto& name = arg.ghost().get_ghost_name();

View File

@ -106,7 +106,7 @@ void gc::mark_var(std::vector<var>& bfs_queue, var& value) {
case vm_type::vm_hash: mark_hash(bfs_queue, value.hash()); break;
case vm_type::vm_func: mark_func(bfs_queue, value.func()); break;
case vm_type::vm_upval: mark_upval(bfs_queue, value.upval()); break;
case vm_type::vm_obj: mark_ghost(bfs_queue, value.ghost()); break;
case vm_type::vm_ghost: mark_ghost(bfs_queue, value.ghost()); break;
case vm_type::vm_co: mark_co(bfs_queue, value.co()); break;
case vm_type::vm_map: mark_map(bfs_queue, value.map()); break;
default: break;
@ -218,7 +218,7 @@ void gc::init(
strs.resize(constant_strings.size());
for(u32 i = 0; i<strs.size(); ++i) {
// incremental initialization, avoid memory leak in repl mode
if (strs[i].type==vm_type::vm_str && strs[i].str()==constant_strings[i]) {
if (strs[i].is_str() && strs[i].str()==constant_strings[i]) {
continue;
}
strs[i] = var::gcobj(new nas_val(vm_type::vm_str));
@ -230,7 +230,7 @@ void gc::init(
env_argv.resize(argv.size());
for(usize i = 0; i<argv.size(); ++i) {
// incremental initialization, avoid memory leak in repl mode
if (env_argv[i].type==vm_type::vm_str && env_argv[i].str()==argv[i]) {
if (env_argv[i].is_str() && env_argv[i].str()==argv[i]) {
continue;
}
env_argv[i] = var::gcobj(new nas_val(vm_type::vm_str));
@ -271,7 +271,7 @@ void gc::info() const {
"hashmap",
"function",
"upvalue",
"object",
"ghost",
"coroutine",
"namespace",
nullptr

View File

@ -48,7 +48,7 @@ var nas_hash::get_value(const std::string& key) {
return ret;
}
for(auto& i : val.vec().elems) {
if (i.type==vm_type::vm_hash) {
if (i.is_hash()) {
ret = i.hash().get_value(key);
}
if (ret.type!=vm_type::vm_none) {
@ -70,7 +70,7 @@ var* nas_hash::get_memory(const std::string& key) {
return addr;
}
for(auto& i : val.vec().elems) {
if (i.type==vm_type::vm_hash) {
if (i.is_hash()) {
addr = i.hash().get_memory(key);
}
if (addr) {
@ -205,7 +205,7 @@ nas_val::nas_val(vm_type val_type) {
case vm_type::vm_hash: ptr.hash = new nas_hash; break;
case vm_type::vm_func: ptr.func = new nas_func; break;
case vm_type::vm_upval: ptr.upval = new nas_upval; break;
case vm_type::vm_obj: ptr.obj = new nas_ghost; break;
case vm_type::vm_ghost: ptr.obj = new nas_ghost; break;
case vm_type::vm_co: ptr.co = new nas_co; break;
case vm_type::vm_map: ptr.map = new nas_map; break;
}
@ -218,7 +218,7 @@ nas_val::~nas_val() {
case vm_type::vm_hash: delete ptr.hash; break;
case vm_type::vm_func: delete ptr.func; break;
case vm_type::vm_upval:delete ptr.upval; break;
case vm_type::vm_obj: delete ptr.obj; break;
case vm_type::vm_ghost:delete ptr.obj; break;
case vm_type::vm_co: delete ptr.co; break;
case vm_type::vm_map: delete ptr.map; break;
}
@ -232,7 +232,7 @@ void nas_val::clear() {
case vm_type::vm_hash: ptr.hash->elems.clear(); break;
case vm_type::vm_func: ptr.func->clear(); break;
case vm_type::vm_upval:ptr.upval->clear(); break;
case vm_type::vm_obj: ptr.obj->clear(); break;
case vm_type::vm_ghost:ptr.obj->clear(); break;
case vm_type::vm_co: ptr.co->clear(); break;
case vm_type::vm_map: ptr.map->clear(); break;
}
@ -263,7 +263,7 @@ std::ostream& operator<<(std::ostream& out, var& ref) {
case vm_type::vm_vec: out << ref.vec(); break;
case vm_type::vm_hash: out << ref.hash(); break;
case vm_type::vm_func: out << "func(..) {..}"; break;
case vm_type::vm_obj: out << ref.ghost(); break;
case vm_type::vm_ghost:out << ref.ghost(); break;
case vm_type::vm_co: out << ref.co(); break;
case vm_type::vm_map: out << ref.map(); break;
}
@ -271,7 +271,7 @@ std::ostream& operator<<(std::ostream& out, var& ref) {
}
bool var::object_check(const std::string& name) {
return type==vm_type::vm_obj && ghost().type_name==name && ghost().pointer;
return is_ghost() && ghost().type_name==name && ghost().pointer;
}
var var::none() {

View File

@ -21,7 +21,7 @@ enum class vm_type: u8 {
vm_hash, // hashmap(dict)
vm_func, // function(lambda)
vm_upval, // upvalue
vm_obj, // ghost type
vm_ghost, // ghost type
vm_co, // coroutine
vm_map, // for globals and namespaces
/* mark type range */
@ -78,6 +78,7 @@ public:
std::string to_str();
bool object_check(const std::string&);
public:
// create new var object
static var none();
static var nil();
@ -87,6 +88,7 @@ public:
static var gcobj(nas_val*);
static var addr(var*);
public:
// get value
var* addr();
u32 ret() const;
@ -100,6 +102,22 @@ public:
nas_ghost& ghost();
nas_co& co();
nas_map& map();
public:
bool is_none() const { return type==vm_type::vm_none; }
bool is_cnt() const { return type==vm_type::vm_cnt; }
bool is_addr() const { return type==vm_type::vm_addr; }
bool is_ret() const { return type==vm_type::vm_ret; }
bool is_nil() const { return type==vm_type::vm_nil; }
bool is_num() const { return type==vm_type::vm_num; }
bool is_str() const { return type==vm_type::vm_str; }
bool is_vec() const { return type==vm_type::vm_vec; }
bool is_hash() const { return type==vm_type::vm_hash; }
bool is_func() const { return type==vm_type::vm_func; }
bool is_upval() const { return type==vm_type::vm_upval; }
bool is_ghost() const { return type==vm_type::vm_ghost; }
bool is_coroutine() const { return type==vm_type::vm_co; }
bool is_map() const { return type==vm_type::vm_map; }
};
struct nas_vec {

View File

@ -91,7 +91,7 @@ void vm::value_info(var& val) {
case vm_type::vm_hash: std::clog << "| hash | <0x" << std::hex << p
<< std::dec << "> {" << val.hash().size()
<< " val}"; break;
case vm_type::vm_obj: std::clog << "| obj | <0x" << std::hex << p
case vm_type::vm_ghost:std::clog << "| obj | <0x" << std::hex << p
<< "> obj:0x"
<< reinterpret_cast<u64>(val.ghost().pointer)
<< std::dec; break;
@ -139,9 +139,7 @@ void vm::function_call_trace() {
// generate trace back
std::stack<const nas_func*> functions;
for(var* i = bottom; i<=top; ++i) {
if (i->type==vm_type::vm_func &&
i-1>=bottom &&
(i-1)->type==vm_type::vm_ret) {
if (i->is_func() && i-1>=bottom && (i-1)->is_ret()) {
functions.push(&i->func());
}
}
@ -179,7 +177,7 @@ void vm::trace_back() {
// generate trace back
std::stack<u32> ret;
for(var* i = ctx.stack; i<=ctx.top; ++i) {
if (i->type==vm_type::vm_ret && i->ret()!=0) {
if (i->is_ret() && i->ret()!=0) {
ret.push(i->ret());
}
}
@ -239,7 +237,7 @@ void vm::register_info() {
}
void vm::global_state() {
if (!global_size || global[0].type==vm_type::vm_none) {
if (!global_size || global[0].is_none()) {
return;
}
std::clog << "\nglobal (0x" << std::hex
@ -269,7 +267,7 @@ void vm::local_state() {
}
void vm::upvalue_state() {
if (ctx.funcr.type==vm_type::vm_nil || ctx.funcr.func().upval.empty()) {
if (ctx.funcr.is_nil() || ctx.funcr.func().upval.empty()) {
return;
}
std::clog << "\nupvalue\n";
@ -329,7 +327,7 @@ std::string vm::report_special_call_lack_arguments(
argument_list[i.second-1] = i.first;
}
for(const auto& key : argument_list) {
if (local[func.keys.at(key)].type==vm_type::vm_none) {
if (local[func.keys.at(key)].is_none()) {
result += key + ", ";
} else {
result += key + "[get], ";
@ -380,7 +378,7 @@ std::string vm::type_name_string(const var& value) const {
case vm_type::vm_hash: return "hash";
case vm_type::vm_func: return "function";
case vm_type::vm_upval: return "upvalue";
case vm_type::vm_obj: return "ghost type";
case vm_type::vm_ghost: return "ghost type";
case vm_type::vm_co: return "coroutine";
case vm_type::vm_map: return "namespace";
}

View File

@ -195,9 +195,9 @@ public:
};
inline bool vm::cond(var& val) {
if (val.type==vm_type::vm_num) {
if (val.is_num()) {
return val.num();
} else if (val.type==vm_type::vm_str) {
} else if (val.is_str()) {
const f64 num = str2num(val.str().c_str());
return std::isnan(num)? !val.str().empty():num;
}
@ -268,7 +268,7 @@ inline void vm::o_newf() {
func.upval = ctx.funcr.func().upval;
// function created in the same local scope shares one closure
// so this size & stk setting has no problem
var upval = (ctx.upvalr.type==vm_type::vm_nil)?
var upval = (ctx.upvalr.is_nil())?
ngc.alloc(vm_type::vm_upval):
ctx.upvalr;
upval.upval().size = ctx.funcr.func().local_size;
@ -363,7 +363,7 @@ inline void vm::o_mul() {op_calc(*);}
inline void vm::o_div() {op_calc(/);}
inline void vm::o_lnk() {
// concat two vectors into one
if (ctx.top[-1].type==vm_type::vm_vec && ctx.top[0].type==vm_type::vm_vec) {
if (ctx.top[-1].is_vec() && ctx.top[0].is_vec()) {
ngc.temp = ngc.alloc(vm_type::vm_vec);
for(auto i : ctx.top[-1].vec().elems) {
ngc.temp.vec().elems.push_back(i);
@ -498,11 +498,11 @@ inline void vm::o_meq() {
inline void vm::o_eq() {
var val2 = ctx.top[0];
var val1 = (--ctx.top)[0];
if (val1.type==vm_type::vm_nil && val2.type==vm_type::vm_nil) {
if (val1.is_nil() && val2.is_nil()) {
ctx.top[0] = one;
} else if (val1.type==vm_type::vm_str && val2.type==vm_type::vm_str) {
} else if (val1.is_str() && val2.is_str()) {
ctx.top[0] = (val1.str()==val2.str())? one:zero;
} else if ((val1.type==vm_type::vm_num || val2.type==vm_type::vm_num)
} else if ((val1.is_num() || val2.is_num())
&& val1.type!=vm_type::vm_nil && val2.type!=vm_type::vm_nil) {
ctx.top[0] = (val1.to_num()==val2.to_num())? one:zero;
} else {
@ -513,11 +513,11 @@ inline void vm::o_eq() {
inline void vm::o_neq() {
var val2 = ctx.top[0];
var val1 = (--ctx.top)[0];
if (val1.type==vm_type::vm_nil && val2.type==vm_type::vm_nil) {
if (val1.is_nil() && val2.is_nil()) {
ctx.top[0] = zero;
} else if (val1.type==vm_type::vm_str && val2.type==vm_type::vm_str) {
} else if (val1.is_str() && val2.is_str()) {
ctx.top[0] = (val1.str()!=val2.str())? one:zero;
} else if ((val1.type==vm_type::vm_num || val2.type==vm_type::vm_num)
} else if ((val1.is_num() || val2.is_num())
&& val1.type!=vm_type::vm_nil && val2.type!=vm_type::vm_nil) {
ctx.top[0] = (val1.to_num()!=val2.to_num())? one:zero;
} else {
@ -613,25 +613,25 @@ inline void vm::o_upval() {
inline void vm::o_callv() {
var val = ctx.top[0];
var vec = (--ctx.top)[0];
if (vec.type==vm_type::vm_vec) {
if (vec.is_vec()) {
ctx.top[0] = vec.vec().get_value(val.to_num());
if (ctx.top[0].type==vm_type::vm_none) {
if (ctx.top[0].is_none()) {
die(report_out_of_range(val.to_num(), vec.vec().size()));
return;
}
} else if (vec.type==vm_type::vm_hash) {
} else if (vec.is_hash()) {
if (val.type!=vm_type::vm_str) {
die("must use string as the key but get "+type_name_string(val));
return;
}
ctx.top[0] = vec.hash().get_value(val.str());
if (ctx.top[0].type==vm_type::vm_none) {
if (ctx.top[0].is_none()) {
die(report_key_not_found(val.str(), vec.hash()));
return;
} else if (ctx.top[0].type==vm_type::vm_func) {
} else if (ctx.top[0].is_func()) {
ctx.top[0].func().local[0] = val; // 'me'
}
} else if (vec.type==vm_type::vm_str) {
} else if (vec.is_str()) {
const auto& str = vec.str();
i32 num = val.to_num();
i32 len = str.length();
@ -642,13 +642,13 @@ inline void vm::o_callv() {
ctx.top[0] = var::num(
static_cast<f64>(static_cast<u8>(str[num>=0? num:num+len]))
);
} else if (vec.type==vm_type::vm_map) {
} else if (vec.is_map()) {
if (val.type!=vm_type::vm_str) {
die("must use string as the key but get "+type_name_string(val));
return;
}
ctx.top[0] = vec.map().get_value(val.str());
if (ctx.top[0].type==vm_type::vm_none) {
if (ctx.top[0].is_none()) {
die("cannot find symbol \""+val.str()+"\"");
return;
}
@ -666,7 +666,7 @@ inline void vm::o_callvi() {
}
// cannot use operator[],because this may cause overflow
(++ctx.top)[0] = val.vec().get_value(imm[ctx.pc]);
if (ctx.top[0].type==vm_type::vm_none) {
if (ctx.top[0].is_none()) {
die(report_out_of_range(imm[ctx.pc], val.vec().size()));
return;
}
@ -679,17 +679,17 @@ inline void vm::o_callh() {
return;
}
const auto& str = const_string[imm[ctx.pc]];
if (val.type==vm_type::vm_hash) {
if (val.is_hash()) {
ctx.top[0] = val.hash().get_value(str);
} else {
ctx.top[0] = val.map().get_value(str);
}
if (ctx.top[0].type==vm_type::vm_none) {
val.type==vm_type::vm_hash?
if (ctx.top[0].is_none()) {
val.is_hash()?
die(report_key_not_found(str, val.hash())):
die("cannot find symbol \"" + str + "\"");
return;
} else if (ctx.top[0].type==vm_type::vm_func) {
} else if (ctx.top[0].is_func()) {
ctx.top[0].func().local[0] = val; // 'me'
}
}
@ -715,7 +715,7 @@ inline void vm::o_callfv() {
}
// parameter size is func->psize-1, 1 is reserved for "me"
const u32 parameter_size = func.parameter_size-1;
if (argc<parameter_size && func.local[argc+1].type==vm_type::vm_none) {
if (argc<parameter_size && func.local[argc+1].is_none()) {
die(report_lack_arguments(argc, func));
return;
}
@ -799,7 +799,7 @@ inline void vm::o_callfh() {
const auto& key = i.first;
if (hash.count(key)) {
local[i.second] = hash.at(key);
} else if (local[i.second].type==vm_type::vm_none) {
} else if (local[i.second].is_none()) {
lack_arguments_flag = true;
}
}
@ -831,7 +831,7 @@ inline void vm::o_callb() {
ctx.top[0] = result;
// if get none, this means errors occurred when calling this native function
if (ctx.top[0].type==vm_type::vm_none) {
if (ctx.top[0].is_none()) {
die("error occurred in native function");
return;
}
@ -858,7 +858,7 @@ inline void vm::o_slcend() {
inline void vm::o_slc() {
var val = (ctx.top--)[0];
var res = ctx.top[-1].vec().get_value(val.to_num());
if (res.type==vm_type::vm_none) {
if (res.is_none()) {
die(report_out_of_range(val.to_num(), ctx.top[-1].vec().size()));
return;
}
@ -876,12 +876,12 @@ inline void vm::o_slc2() {
i32 num1 = val1.to_num();
i32 num2 = val2.to_num();
i32 size = ref.size();
if (type1==vm_type::vm_nil && type2==vm_type::vm_nil) {
if (val1.is_nil() && val2.is_nil()) {
num1 = 0;
num2 = size-1;
} else if (type1==vm_type::vm_nil && type2!=vm_type::vm_nil) {
} else if (val1.is_nil() && type2!=vm_type::vm_nil) {
num1 = num2<0? -size:0;
} else if (type1!=vm_type::vm_nil && type2==vm_type::vm_nil) {
} else if (type1!=vm_type::vm_nil && val2.is_nil()) {
num2 = num1<0? -1:size-1;
}
@ -926,13 +926,13 @@ inline void vm::o_mupval() {
inline void vm::o_mcallv() {
var val = ctx.top[0]; // index
var vec = (--ctx.top)[0]; // mcall vector, reserved on stack to avoid gc
if (vec.type==vm_type::vm_vec) {
if (vec.is_vec()) {
ctx.memr = vec.vec().get_memory(val.to_num());
if (!ctx.memr) {
die(report_out_of_range(val.to_num(), vec.vec().size()));
return;
}
} else if (vec.type==vm_type::vm_hash) { // do mcallh but use the mcallv way
} else if (vec.is_hash()) { // do mcallh but use the mcallv way
if (val.type!=vm_type::vm_str) {
die("must use string as the key but get "+type_name_string(val));
return;
@ -944,7 +944,7 @@ inline void vm::o_mcallv() {
ref.elems[str] = nil;
ctx.memr = ref.get_memory(str);
}
} else if (vec.type==vm_type::vm_map) {
} else if (vec.is_map()) {
if (val.type!=vm_type::vm_str) {
die("must use string as the key but get "+type_name_string(val));
return;
@ -968,7 +968,7 @@ inline void vm::o_mcallh() {
return;
}
const auto& str = const_string[imm[ctx.pc]];
if (hash.type==vm_type::vm_map) {
if (hash.is_map()) {
ctx.memr = hash.map().get_memory(str);
if (!ctx.memr) {
die("cannot find symbol \"" + str + "\"");
@ -1013,7 +1013,7 @@ inline void vm::o_ret() {
ctx.top[0] = ret; // rewrite func with returned value
// synchronize upvalue
if (up.type==vm_type::vm_upval) {
if (up.is_upval()) {
auto& upval = up.upval();
auto size = func.func().local_size;
upval.on_stack = false;

View File

@ -40,7 +40,7 @@ var builtin_fork(context* ctx, gc* ngc) {
var builtin_waitpid(context* ctx, gc* ngc) {
auto pid = ctx->localr[1];
auto nohang = ctx->localr[2];
if (pid.type!=vm_type::vm_num || nohang.type!=vm_type::vm_num) {
if (!pid.is_num() || !nohang.is_num()) {
return nas_err("unix::waitpid", "pid and nohang must be number");
}
#ifndef _WIN32
@ -56,7 +56,7 @@ var builtin_waitpid(context* ctx, gc* ngc) {
var builtin_opendir(context* ctx, gc* ngc) {
auto path = ctx->localr[1];
if (path.type!=vm_type::vm_str) {
if (!path.is_str()) {
return nas_err("unix::opendir", "\"path\" must be string");
}
#ifdef _MSC_VER
@ -72,7 +72,7 @@ var builtin_opendir(context* ctx, gc* ngc) {
return nas_err("unix::opendir", "cannot open dir <"+path.str()+">");
}
#endif
var ret = ngc->alloc(vm_type::vm_obj);
var ret = ngc->alloc(vm_type::vm_ghost);
ret.ghost().set(dir_type_name, dir_entry_destructor, nullptr, p);
return ret;
}
@ -105,7 +105,7 @@ var builtin_closedir(context* ctx, gc* ngc) {
var builtin_chdir(context* ctx, gc* ngc) {
auto path = ctx->localr[1];
if (path.type!=vm_type::vm_str) {
if (!path.is_str()) {
return var::num(-1.0);
}
return var::num(static_cast<f64>(chdir(path.str().c_str())));
@ -131,7 +131,7 @@ var builtin_getcwd(context* ctx, gc* ngc) {
var builtin_getenv(context* ctx, gc* ngc) {
auto envvar = ctx->localr[1];
if (envvar.type!=vm_type::vm_str) {
if (!envvar.is_str()) {
return nas_err("unix::getenv", "\"envvar\" must be string");
}
char* res = getenv(envvar.str().c_str());