🎨 open return nil if fails

This commit is contained in:
ValKmjolnir 2025-02-15 14:24:08 +08:00
parent b177a58662
commit 065f6ae162
2 changed files with 11 additions and 7 deletions

View File

@ -381,7 +381,10 @@ var builtin_substr(context* ctx, gc* ngc) {
auto begin = static_cast<usize>(beg.num()); auto begin = static_cast<usize>(beg.num());
auto length = static_cast<usize>(len.num()); auto length = static_cast<usize>(len.num());
if (begin >= str.str().length()) { if (begin >= str.str().length()) {
return nas_err("native::susbtr", "begin index out of range: "+std::to_string(begin)); return nas_err(
"native::susbtr",
"begin index out of range: " + std::to_string(begin)
);
} }
return ngc->newstr(str.str().substr(begin, length)); return ngc->newstr(str.str().substr(begin, length));
} }

View File

@ -59,8 +59,9 @@ var builtin_open(context* ctx, gc* ngc) {
return nas_err("io::open", "\"mode\" must be string"); return nas_err("io::open", "\"mode\" must be string");
} }
auto file_descriptor = fopen(name.str().c_str(), mode.str().c_str()); auto file_descriptor = fopen(name.str().c_str(), mode.str().c_str());
// if failed to open, just return nil for check
if (!file_descriptor) { if (!file_descriptor) {
return nas_err("io::open", "failed to open file <" + name.str() + ">"); return nil;
} }
var return_object = ngc->alloc(vm_type::vm_ghost); var return_object = ngc->alloc(vm_type::vm_ghost);
return_object.ghost().set( return_object.ghost().set(