Skip to content

Commit

Permalink
refactor require
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Apr 5, 2012
1 parent d3840ba commit a2799d1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 27 deletions.
11 changes: 6 additions & 5 deletions TODO
Expand Up @@ -123,7 +123,6 @@ feature
* ~~
* bignum support using ttmath?
* for (my $i=0; $i<100; $i++) { } say($i); should be fail.
* use ICU for converting encoding.
* local $n
* check deep recursion
* traits
Expand All @@ -142,16 +141,13 @@ Library
-------

* Socket
* flock
* readline
* ANSIColor
* HTTP client
* HTTP server
* B::Deparse
* Test::More
* dir()
* file()
* Text::MicroTemplate
* implement Path::Class detial.

Finished
--------
Expand Down Expand Up @@ -249,3 +245,8 @@ Bytes#decode() / with icu?

String#encode()
open()
* dir()
* file()
* flock
* HTTP server
* use ICU for converting encoding.
4 changes: 4 additions & 0 deletions proposals/001-no-global-namespace.mkdn
Expand Up @@ -73,4 +73,8 @@ You can call it by following form.

my $foo = Foo.Foo.new();

Scoping
-------

Class and Function may register to file scope context.

6 changes: 5 additions & 1 deletion tora/builtin.cc
Expand Up @@ -10,6 +10,7 @@
#include "value/object.h"
#include "value/pointer.h"
#include "value/array.h"
#include "value/symbol.h"
#include "value/str.h"
#include "value/exception.h"
#include "value.h"
Expand Down Expand Up @@ -119,7 +120,10 @@ static SharedPtr<Value> builtin_self(VM *vm) {
* require("foo/bar.tra")
*/
static SharedPtr<Value> builtin_require(VM *vm, Value *v) {
return vm->require(v);
ID id = v->upcast<SymbolValue>()->id();
std::string name = vm->symbol_table->id2name(id);
vm->require_package(name);
return UndefValue::instance();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tora/frame.cc
Expand Up @@ -11,7 +11,7 @@ using namespace tora;
SharedPtr<Value> LexicalVarsFrame::get_variable(int id) const {
#ifndef NDEBUG
if (id >= this->vars.capacity()) {
fprintf(stderr, "[BUG] id< vars.capacity(). id: %d, capacity: %ld\n", (long int) id, (long int) this->vars.capacity());
fprintf(stderr, "[BUG] id< vars.capacity(). id: %ld, capacity: %ld\n", (long int) id, (long int) this->vars.capacity());
abort();
}
#endif
Expand Down
23 changes: 14 additions & 9 deletions tora/vm.cc
Expand Up @@ -240,12 +240,19 @@ static SharedPtr<Value> builtin_do(VM * vm, Value *v) {
}
}

SharedPtr<Value> VM::require(Value * v) {
void VM::use(Value * package_v, bool need_copy) {
std::string package = symbol_table->id2name(package_v->upcast<SymbolValue>()->id());
this->require_package(package);
if (need_copy) {
this->copy_all_public_symbols(package_v->upcast<SymbolValue>()->id(), this->package_id());
}
}

void VM::require_package(const std::string &package) {
VM *vm = this;
SharedPtr<ArrayValue> libpath = vm->global_vars->at(2)->upcast<ArrayValue>();
SharedPtr<HashValue> required = vm->global_vars->at(3)->upcast<HashValue>();
std::string s = symbol_table->id2name(v->upcast<SymbolValue>()->id());
std::string package = s;
std::string s = package;
{
auto iter = s.find("::");
while (iter != std::string::npos) {
Expand All @@ -260,7 +267,7 @@ SharedPtr<Value> VM::require(Value * v) {
if (required->get(s)->value_type == VALUE_TYPE_UNDEF) {
throw new StrValue("Compilation failed in require");
} else {
return new IntValue(1);
return;
}
}

Expand All @@ -276,8 +283,8 @@ SharedPtr<Value> VM::require(Value * v) {
required->set_item(new StrValue(s), realfilename_value);
std::ifstream ifs(realfilename.c_str());
if (ifs.is_open()) {
SharedPtr<Value> ret = eval_foo(vm, &ifs, package, realfilename).get();
return ret;
(void)eval_foo(vm, &ifs, package, realfilename).get();
return;
} else {
required->set_item(new StrValue(s), UndefValue::instance());
throw new ExceptionValue(realfilename + " : " + get_strerror(get_errno()));
Expand Down Expand Up @@ -412,7 +419,7 @@ void VM::register_standard_methods() {
this->add_builtin_function("do", builtin_do);
}

SharedPtr<Value> VM::copy_all_public_symbols(ID srcid, ID dstid) {
void VM::copy_all_public_symbols(ID srcid, ID dstid) {
SharedPtr<Package> srcpkg = this->find_package(srcid);
SharedPtr<Package> dstpkg = this->find_package(dstid);

Expand All @@ -427,8 +434,6 @@ SharedPtr<Value> VM::copy_all_public_symbols(ID srcid, ID dstid) {
// copy non-code value to other package?
}
}
// dstpkg->dump(this->symbol_table, 1);
return UndefValue::instance();
}

void VM::add_function(ID pkgid, ID id, SharedPtr<Value> code) {
Expand Down
5 changes: 3 additions & 2 deletions tora/vm.h
Expand Up @@ -103,7 +103,8 @@ class VM {

void dump_frame();
void dump_stack();
SharedPtr<Value> require(Value *v);
void use(Value *v, bool);
void require_package(const std::string &);
void add_function(ID pkgid, ID id, SharedPtr<Value> code);
void add_function(ID pkgid, std::string &name, SharedPtr<Value> code) {
this->add_function(pkgid, this->symbol_table->get_id(name), code);
Expand All @@ -126,7 +127,7 @@ class VM {
return ops->at(pc)->operand.double_value;
}

SharedPtr<Value> copy_all_public_symbols(ID srcid, ID dstid);
void copy_all_public_symbols(ID srcid, ID dstid);
SharedPtr<Value> get_self();

const SharedPtr<Value>& TOP() { return stack.back(); }
Expand Down
15 changes: 6 additions & 9 deletions vm.inc
Expand Up @@ -71,7 +71,9 @@ OP_NEW_RANGE {
}

OP_FUNCDEF {
SharedPtr<Value> code = stack.back(); stack.pop_back(); // code object
SharedPtr<Value> code = stack.back();
stack.pop_back();

assert(code->value_type == VALUE_TYPE_CODE);
// printf("FUNCDEF!! %d, %d\n", package_id(), code->upcast<CodeValue>()->func_name_id);
int pkgid = code->upcast<CodeValue>()->package_id();
Expand Down Expand Up @@ -947,14 +949,9 @@ OP_USE {
SharedPtr<Value> include = stack.back();
stack.pop_back();

SharedPtr<Value> ret = this->require(mod_name.get());
if (include->value_type == VALUE_TYPE_INT && include->upcast<IntValue>()->int_value() == 1) {
SharedPtr<Value> v = this->copy_all_public_symbols(mod_name->upcast<SymbolValue>()->id(), this->package_id());
if (v->is_exception()) {
this->die(ret);
}
}
stack.push_back(ret);
bool is_copy_all = include->value_type == VALUE_TYPE_INT && include->upcast<IntValue>()->int_value() == 1;
this->use(mod_name.get(), is_copy_all);
stack.push_back(UndefValue::instance());
}

OP_NOT {
Expand Down

0 comments on commit a2799d1

Please sign in to comment.