Permalink
Browse files

no more global namespace. you can use python-ish class namespace.

  • Loading branch information...
tokuhirom committed Apr 7, 2012
1 parent e5f7275 commit 21d42f6a43629681fcd084d001674abb24e32bd4
Showing with 445 additions and 228 deletions.
  1. +1 −0 SConstruct
  2. +9 −9 ext/Curl/curl.cc
  3. +1 −1 ext/Curl/t/curl.tra
  4. +1 −7 ext/Digest-MD5/Digest-MD5.cc
  5. +1 −1 ext/Digest-MD5/t/test.tra
  6. +6 −10 ext/Fcntl/fcntl.cc
  7. +4 −5 ext/Fcntl/t/00_load.tra
  8. +5 −7 ext/HTTP/http.cc
  9. +2 −2 ext/HTTP/lib/HTTP/Client.tra
  10. +2 −2 ext/HTTP/t/client.tra
  11. +1 −1 ext/HTTP/t/http.tra
  12. +1 −1 ext/HTTP/t/response.tra
  13. +4 −10 ext/JSON/json.cc
  14. +1 −1 ext/JSON/t/decode.tra
  15. +1 −1 ext/Path/lib/Path.tra
  16. +2 −4 ext/Path/path.cc
  17. +1 −1 ext/Path/t/01_mkpath.tra
  18. +1 −1 ext/Path/t/02_path.tra
  19. +19 −17 ext/Socket/socket.cc
  20. +2 −2 ext/Socket/t/01_const.tra
  21. +2 −8 ext/URI-Escape/URI-Escape.cc
  22. +1 −1 ext/URI-Escape/lib/URI/Escape.tra
  23. +1 −1 ext/URI-Escape/t/01-uri.tra
  24. +1 −1 lib/XSLoader.tra
  25. +1 −1 notes/closure.tra
  26. +8 −0 proposals/001-no-global-namespace.mkdn
  27. +1 −1 t/tra/__file__.tra
  28. +1 −1 t/tra/anonfor.tra
  29. +1 −1 t/tra/array.tra
  30. +1 −1 t/tra/binary_assign.tra
  31. +1 −1 t/tra/builtins/callee.tra
  32. +1 −1 t/tra/builtins/getpid.tra
  33. +1 −1 t/tra/builtins/getppid.tra
  34. +1 −1 t/tra/builtins/math.tra
  35. +1 −1 t/tra/caller.tra
  36. +1 −1 t/tra/class.tra
  37. +1 −1 t/tra/code.tra
  38. +1 −1 t/tra/config.tra
  39. +1 −1 t/tra/double.tra
  40. +1 −1 t/tra/filetest.tra
  41. +1 −1 t/tra/getcwd.tra
  42. +1 −1 t/tra/global_vars.tra
  43. +1 −1 t/tra/hash.tra
  44. +1 −0 t/tra/import/lib/IMP3.tra
  45. +7 −0 t/tra/import/no_import_funcall.tra
  46. +1 −1 t/tra/import/simple.tra
  47. +1 −1 t/tra/lambda.tra
  48. +1 −1 t/tra/last.tra
  49. +1 −1 t/tra/lib/uri-split.tra
  50. +1 −1 t/tra/object/array.tra
  51. +1 −1 t/tra/object/bytes.tra
  52. +1 −1 t/tra/object/code.tra
  53. +1 −1 t/tra/object/dir.tra
  54. +1 −1 t/tra/object/file.tra
  55. +1 −1 t/tra/object/hash.tra
  56. +3 −1 t/tra/object/meta.tra
  57. +1 −1 t/tra/object/regexp.tra
  58. +1 −1 t/tra/object/string.tra
  59. +1 −1 t/tra/oop/destroy.tra
  60. +2 −1 t/tra/oop/inheritance.tra
  61. +1 −1 t/tra/oop/self-bug.tra
  62. +1 −1 t/tra/optargs.tra
  63. +1 −1 t/tra/postfix-for.tra
  64. +1 −1 t/tra/range.tra
  65. +1 −1 t/tra/return.tra
  66. +1 −1 t/tra/sprintf.tra
  67. +1 −1 t/tra/string-copy.tra
  68. +1 −1 t/tra/string-embed-var.tra
  69. +1 −1 t/tra/swap.tra
  70. +1 −1 t/tra/syntax/arguments.tra
  71. +1 −1 t/tra/syntax/flexible_args.tra
  72. +1 −1 t/tra/syntax/method.tra
  73. +1 −1 t/tra/syntax/oror_assign.tra
  74. +1 −1 t/tra/syntax/postfix-while.tra
  75. +1 −1 t/tra/while-multi.tra
  76. +9 −7 tora/compiler.cc
  77. +1 −1 tora/inspector.cc
  78. +1 −0 tora/main.cc
  79. +2 −0 tora/nodes.gen.pl
  80. +5 −1 tora/object.cc
  81. +2 −0 tora/object.h
  82. +7 −47 tora/object/class.cc
  83. +27 −0 tora/object/file_package.cc
  84. +91 −0 tora/object/metaclass.cc
  85. +20 −3 tora/object/object.cc
  86. +5 −2 tora/parser.yy
  87. +8 −0 tora/peek.cc
  88. +2 −0 tora/symbols.gen.pl
  89. +2 −2 tora/tora.h
  90. +7 −0 tora/value.cc
  91. +1 −1 tora/value.h
  92. +17 −2 tora/value/file_package.h
  93. +67 −14 tora/vm.cc
  94. +1 −1 tora/vm.gen.pl
  95. +5 −2 tora/vm.h
  96. +1 −0 tora/vm.ops.inc.h
  97. +27 −3 vm.inc
View
@@ -144,6 +144,7 @@ libfiles = [
object/bytes.cc object/regexp.cc object/hash.cc
object/re2_regexp_matched.cc
object/int.cc object/double.cc
+ object/metaclass.cc object/file_package.cc
''')
]
View
@@ -2,7 +2,7 @@
#include <vm.h>
#include <value/object.h>
#include <value/pointer.h>
-#include <package.h>
+#include <value/class.h>
#include <shared_ptr.h>
#include <curl/curl.h>
@@ -17,21 +17,21 @@ inline CURL *SELF(Value *self) {
}
static SharedPtr<Value> Curl_Easy_new(VM *vm, Value *klass) {
- return new ObjectValue(vm, vm->symbol_table->get_id("Curl::Easy"), new PointerValue(curl_easy_init()));
+ assert(klass->value_type == VALUE_TYPE_CLASS);
+ return new ObjectValue(vm, klass->upcast<ClassValue>(), new PointerValue(curl_easy_init()));
}
static SharedPtr<Value> Curl_Easy_DESTROY(VM *vm, Value *self) {
curl_easy_cleanup(SELF(self));
+ return UndefValue::instance();
}
-extern "C" {
-
TORA_EXPORT
void Init_Curl(VM* vm) {
- SharedPtr<Package> pkg = vm->find_package("Curl::Easy");
- pkg->add_method(vm->symbol_table->get_id("new"), new CallbackFunction(Curl_Easy_new));
- pkg->add_method(vm->symbol_table->get_id("DESTROY"), new CallbackFunction(Curl_Easy_DESTROY));
- pkg->add_constant("CURLOPT_URL", CURLOPT_URL);
+ SharedPtr<ClassValue> klass = new ClassValue(vm, "Curl::Easy");
+ klass->add_method(vm->symbol_table->get_id("new"), new CallbackFunction(Curl_Easy_new));
+ klass->add_method(vm->symbol_table->get_id("DESTROY"), new CallbackFunction(Curl_Easy_DESTROY));
+ klass->add_constant("CURLOPT_URL", CURLOPT_URL);
+ vm->add_class(klass);
}
-}
View
@@ -16,5 +16,5 @@ p($buf);
ok(1);
-done_testing;
+done_testing();
@@ -4,7 +4,6 @@
#include <value/array.h>
#include <value/hash.h>
#include <value/exception.h>
-#include <package.h>
using namespace tora;
@@ -34,12 +33,7 @@ static SharedPtr<Value> Digest_MD5_md5_hex(VM *vm, Value *str) {
return new StrValue(ret);
}
-extern "C" {
-
TORA_EXPORT
void Init_Digest_MD5(VM* vm) {
- SharedPtr<Package> pkg = vm->find_package("Digest::MD5");
- pkg->add_method(vm->symbol_table->get_id("md5_hex"), new CallbackFunction(Digest_MD5_md5_hex));
-}
-
+ vm->add_function("md5_hex", new CallbackFunction(Digest_MD5_md5_hex));
}
@@ -3,4 +3,4 @@ use Digest::MD5 *;
is(md5_hex('hoge'), 'ea703e7aa1efda0064eaa507d9e8ab7e');
-done_testing;
+done_testing();
View
@@ -5,11 +5,11 @@
#include <value/array.h>
#include <value/hash.h>
#include <value/exception.h>
-#include <package.h>
+#include <value/class.h>
using namespace tora;
-static SharedPtr<Value> Fcntl_flock(VM *vm, Value *str, Value *fileno_v, Value *operation_v) {
+static SharedPtr<Value> Fcntl_flock(VM *vm, Value *fileno_v, Value *operation_v) {
if (fileno_v->value_type != VALUE_TYPE_INT) {
throw new ExceptionValue("You passed non-integer value for Fcntl.flock() argument 1 : %s", fileno_v->type_str());
}
@@ -24,15 +24,11 @@ static SharedPtr<Value> Fcntl_flock(VM *vm, Value *str, Value *fileno_v, Value *
}
}
-extern "C" {
-
TORA_EXPORT
void Init_Fcntl(VM* vm) {
- SharedPtr<Package> pkg = vm->find_package("Fcntl");
- pkg->add_method("flock", new CallbackFunction(Fcntl_flock));
- pkg->add_constant("LOCK_SH", LOCK_SH);
- pkg->add_constant("LOCK_EX", LOCK_EX);
- pkg->add_constant("LOCK_UN", LOCK_UN);
+ vm->add_function("flock", new CallbackFunction(Fcntl_flock));
+ vm->add_constant("LOCK_SH", LOCK_SH);
+ vm->add_constant("LOCK_EX", LOCK_EX);
+ vm->add_constant("LOCK_UN", LOCK_UN);
}
-}
View
@@ -1,12 +1,11 @@
use Test::More *;
use Fcntl;
-ok(Fcntl.meta.has_method('LOCK_SH'));
-ok(Fcntl.meta.has_method('LOCK_EX'));
-ok(Fcntl.meta.has_method('LOCK_UN'));
-ok(Fcntl.meta.has_method('flock'));
+ok(Fcntl.LOCK_SH);
+ok(Fcntl.LOCK_EX);
+ok(Fcntl.LOCK_UN);
my $fh = open('t/00_load.tra');
Fcntl.flock($fh.fileno, Fcntl.LOCK_EX);
-done_testing;
+done_testing();
View
@@ -6,7 +6,6 @@
#include <value/array.h>
#include <value/hash.h>
#include <value/tuple.h>
-#include <package.h>
#include <shared_ptr.h>
#define MAX_HEADER_NAME_LEN 1024
@@ -313,12 +312,11 @@ extern "C" {
TORA_EXPORT
void Init_HTTP_Parser(VM* vm) {
- SharedPtr<Package> pkg = vm->find_package("HTTP::Parser");
- pkg->add_method("parse_http_request", new CallbackFunction(HTTP_Parser_parse_http_request));
- pkg->add_method("parse_http_response", new CallbackFunction(HTTP_Parser_parse_http_response));
- pkg->add_constant("HEADERS_NONE", HEADERS_NONE);
- pkg->add_constant("HEADERS_AS_HASHREF", HEADERS_AS_HASHREF);
- pkg->add_constant("HEADERS_AS_ARRAYREF", HEADERS_AS_ARRAYREF);
+ vm->add_function("parse_http_request", new CallbackFunction(HTTP_Parser_parse_http_request));
+ vm->add_function("parse_http_response", new CallbackFunction(HTTP_Parser_parse_http_response));
+ vm->add_constant("HEADERS_NONE", HEADERS_NONE);
+ vm->add_constant("HEADERS_AS_HASHREF", HEADERS_AS_HASHREF);
+ vm->add_constant("HEADERS_AS_ARRAYREF", HEADERS_AS_ARRAYREF);
}
}
@@ -1,6 +1,6 @@
use Socket *;
use HTTP::Parser *;
-use HTTP::Response;
+use HTTP::Response *;
use URI::Split *;
class HTTP::Client {
@@ -33,7 +33,7 @@ class HTTP::Client {
my $content = $src.substr($ret);
return HTTP::Response.new($minor_version, $status, $message, $headers, $content);
} else {
- die "Invalid uri: $uri";
+ die "Invalid uri: $url";
}
}
}
View
@@ -1,4 +1,4 @@
-use HTTP::Client;
+use HTTP::Client *;
use URI::Split *;
use Socket *;
use HTTP::Parser *;
@@ -8,4 +8,4 @@ my $h = HTTP::Client.new();
my $res = $h.get('http://example.com/');
is($res.status, 302);
-done_testing;
+done_testing();
View
@@ -14,4 +14,4 @@ use HTTP::Parser *;
is($env.tora(), {"PATH_INFO" => "/","QUERY_STRING" => "","REQUEST_METHOD" => "GET","REQUEST_URI" => "/","SCRIPT_NAME" => "","SERVER_PROTOCOL" => "HTTP/1.0"}.tora());
}
-done_testing;
+done_testing();
View
@@ -9,4 +9,4 @@ is($message, b"OK");
is($headers[0], b'host');
is($headers[1], b'example.com');
-done_testing;
+done_testing();
View
@@ -4,7 +4,6 @@
#include <vm.h>
#include <value/array.h>
#include <value/hash.h>
-#include <package.h>
#include "picojson.h"
using namespace tora;
@@ -71,7 +70,7 @@ static Value* json_encode2(std::string & buf, Value *v) {
*
* This method encode $value to JSON format. and return a string.
*/
-static SharedPtr<Value> json_encode(VM *vm, Value *klass, Value* v) {
+static SharedPtr<Value> json_encode(VM *vm, Value* v) {
std::string buf;
json_encode2(buf, v);
return new StrValue(buf);
@@ -108,7 +107,7 @@ static SharedPtr<Value> json_decode2(picojson::value & v) {
*
* Decode $json to Hash|Array.
*/
-static SharedPtr<Value> json_decode(VM *vm, Value *klass, Value* json_v) {
+static SharedPtr<Value> json_decode(VM *vm, Value* json_v) {
StrValue * json_sv = static_cast<StrValue*>(json_v);
picojson::value v;
const char * in = json_sv->str_value().c_str();
@@ -119,13 +118,8 @@ static SharedPtr<Value> json_decode(VM *vm, Value *klass, Value* json_v) {
return json_decode2(v);
}
-extern "C" {
-
TORA_EXPORT
void Init_JSON(VM* vm) {
- SharedPtr<Package> pkg = vm->find_package("JSON");
- pkg->add_method(vm->symbol_table->get_id("encode"), new CallbackFunction(json_encode));
- pkg->add_method(vm->symbol_table->get_id("decode"), new CallbackFunction(json_decode));
-}
-
+ vm->add_function(vm->symbol_table->get_id("encode"), new CallbackFunction(json_encode));
+ vm->add_function(vm->symbol_table->get_id("decode"), new CallbackFunction(json_decode));
}
View
@@ -15,4 +15,4 @@ is(JSON.decode("{}").tora(), {}.tora);
is(JSON.decode("{\"foo\":3}").tora(), {foo => 3}.tora);
is(JSON.decode("[1,3,\"hoge\"]").tora(), [1,3,'hoge'].tora);
-done_testing;
+done_testing();
View
@@ -8,7 +8,7 @@ sub file($path) {
Path::File.new($path);
}
-$Path::SEPCHAR='/';
+my $SEPCHAR='/';
class Path::Base {
sub stat() {
View
@@ -4,7 +4,6 @@
#include <value/array.h>
#include <value/hash.h>
#include <value/exception.h>
-#include <package.h>
#include <boost/filesystem.hpp>
@@ -39,9 +38,8 @@ extern "C" {
TORA_EXPORT
void Init_File_Path(VM* vm) {
- SharedPtr<Package> pkg = vm->find_package("File::Path");
- pkg->add_method("mkpath", new CallbackFunction(File_Path_mkpath));
- pkg->add_method("rmtree", new CallbackFunction(File_Path_rmtree));
+ vm->add_function("mkpath", new CallbackFunction(File_Path_mkpath));
+ vm->add_function("rmtree", new CallbackFunction(File_Path_rmtree));
}
}
View
@@ -23,4 +23,4 @@ ok(!-e 't/tmp/foo/bar/');
ok(!-e 't/tmp/foo/');
ok(-d 't/tmp/');
-done_testing;
+done_testing();
View
@@ -10,4 +10,4 @@ ok(-d 't/tmp/foo/bar/baz');
dir('t/tmp/foo').rmtree();
ok(!-e 't/tmp/foo');
-done_testing;
+done_testing();
View
@@ -4,7 +4,7 @@
#include "value/array.h"
#include "value/tuple.h"
#include "value/bytes.h"
-#include "package.h"
+#include "value/class.h"
#include <sys/types.h>
#ifdef _WIN32
@@ -59,7 +59,7 @@ static SharedPtr<Value> sock_socket(VM * vm, Value* klass, Value* domain_v, Valu
if (sock == -1) {
throw new ErrnoExceptionValue(get_errno());
} else {
- return new ObjectValue(vm, vm->symbol_table->get_id("Socket::Socket"), new IntValue(sock));
+ return new ObjectValue(vm, vm->get_class(vm->get_id("Socket::Socket")), new IntValue(sock));
}
}
@@ -301,7 +301,7 @@ static SharedPtr<Value> sock_sock_accept(VM * vm, Value* self) {
int fd = accept(GETFD(vm, self), (struct sockaddr *)namebuf, &len);
if (fd >= 0) {
- SharedPtr<ObjectValue> new_sock = new ObjectValue(vm, vm->symbol_table->get_id("Socket::Socket"), new IntValue(fd));
+ SharedPtr<ObjectValue> new_sock = new ObjectValue(vm, vm->get_class(vm->symbol_table->get_id("Socket::Socket")), new IntValue(fd));
SharedPtr<TupleValue> t = new TupleValue();
t->push_back(new StrValue(std::string(namebuf, len)));
t->push_back(new_sock);
@@ -327,23 +327,25 @@ void Init_Socket(VM* vm) {
#endif
{
- SharedPtr<Package> pkg = vm->find_package("Socket");
- pkg->add_method(vm->symbol_table->get_id("socket"), new CallbackFunction(sock_socket));
- pkg->add_method(vm->symbol_table->get_id("sockaddr_in"), new CallbackFunction(sock_sockaddr_in));
- pkg->add_method(vm->symbol_table->get_id("inet_aton"), new CallbackFunction(sock_inet_aton));
+ SharedPtr<ClassValue> klass = new ClassValue(vm, "Socket");
+ klass->add_method("socket", new CallbackFunction(sock_socket));
+ klass->add_method("sockaddr_in", new CallbackFunction(sock_sockaddr_in));
+ klass->add_method("inet_aton", new CallbackFunction(sock_inet_aton));
+ vm->add_class(klass);
}
{
- SharedPtr<Package> pkg = vm->find_package("Socket::Socket");
- pkg->add_method(vm->symbol_table->get_id("connect"), new CallbackFunction(sock_sock_connect));
- pkg->add_method(vm->symbol_table->get_id("write"), new CallbackFunction(sock_write));
- pkg->add_method(vm->symbol_table->get_id("read"), new CallbackFunction(sock_read));
- pkg->add_method(vm->symbol_table->get_id("DESTROY"), new CallbackFunction(sock_sock_DESTROY));
- pkg->add_method(vm->symbol_table->get_id("bind"), new CallbackFunction(sock_sock_bind));
- pkg->add_method(vm->symbol_table->get_id("listen"), new CallbackFunction(sock_sock_listen));
- pkg->add_method(vm->symbol_table->get_id("close"), new CallbackFunction(sock_sock_close));
- pkg->add_method(vm->symbol_table->get_id("accept"), new CallbackFunction(sock_sock_accept));
- pkg->add_method(vm->symbol_table->get_id("setsockopt"), new CallbackFunction(sock_sock_setsockopt));
+ SharedPtr<ClassValue> klass = new ClassValue(vm, "Socket::Socket");
+ klass->add_method("connect", new CallbackFunction(sock_sock_connect));
+ klass->add_method("write", new CallbackFunction(sock_write));
+ klass->add_method("read", new CallbackFunction(sock_read));
+ klass->add_method("DESTROY", new CallbackFunction(sock_sock_DESTROY));
+ klass->add_method("bind", new CallbackFunction(sock_sock_bind));
+ klass->add_method("listen", new CallbackFunction(sock_sock_listen));
+ klass->add_method("close", new CallbackFunction(sock_sock_close));
+ klass->add_method("accept", new CallbackFunction(sock_sock_accept));
+ klass->add_method("setsockopt", new CallbackFunction(sock_sock_setsockopt));
+ vm->add_class(klass);
}
}
@@ -1,8 +1,8 @@
use Test::More *;
use Socket *;
-ok(Socket.meta.has_method('AF_INET'));
+ok(AF_INET);
ok(Socket.meta.has_method('socket'));
-done_testing;
+done_testing();
@@ -16,15 +16,9 @@ static SharedPtr<Value> uri_unescape(VM *vm, Value *val) {
return new StrValue(nu_unescape_uri(str->str_value()));
}
-extern "C" {
-
TORA_EXPORT
void Init_URI_Escape(VM *vm) {
- SharedPtr<ClassValue> klass = new ClassValue(vm, "URI::Escape");
- klass->add_method(vm->symbol_table->get_id("uri_escape"), new CallbackFunction(uri_escape));
- klass->add_method(vm->symbol_table->get_id("uri_unescape"), new CallbackFunction(uri_unescape));
- vm->add_class(klass);
-}
-
+ vm->add_function("uri_escape", new CallbackFunction(uri_escape));
+ vm->add_function("uri_unescape", new CallbackFunction(uri_unescape));
}
@@ -1,6 +1,6 @@
use XSLoader;
-XSLoader.load_xs('URI::Escape');
+XSLoader.load('URI::Escape');
__END__
@@ -7,4 +7,4 @@ is(URI::Escape::uri_escape('ほげ'), "%e3%81%bb%e3%81%92");
is(uri_unescape('%E3%81%BB%E3%81%92'), 'ほげ');
-done_testing;
+done_testing();
Oops, something went wrong.

0 comments on commit 21d42f6

Please sign in to comment.