Permalink
Browse files

ruby proxy is working

  • Loading branch information...
1 parent 9b04337 commit 2874870d970823b0de9ecef816bced9a1eed34d4 @tenderlove committed Sep 23, 2009
View
@@ -2,9 +2,11 @@
VALUE cPhubyArray;
-VALUE Data_Wrap_PhubyArray(zval * value)
+VALUE Data_Wrap_PhubyArray(VALUE rt, zval * value)
{
- return Data_Wrap_Struct(cPhubyArray, 0, 0, value);
+ VALUE arry = Data_Wrap_Struct(cPhubyArray, 0, 0, value);
+ rb_iv_set(arry, "@runtime", rt);
+ return arry;
}
static VALUE length(VALUE self)
@@ -29,7 +31,7 @@ static VALUE get(VALUE self, VALUE key)
RSTRING_LEN(key) + 1, // Add one for the NULL byte
(void **)&value
)) {
- return ZVAL2VALUE(*value);
+ return ZVAL2VALUE(rb_iv_get(self, "@runtime"), *value);
}
return Qnil;
@@ -43,7 +45,7 @@ static VALUE set(VALUE self, VALUE key, VALUE value)
add_assoc_zval(array,
StringValuePtr(key),
- VALUE2ZVAL(value)
+ VALUE2ZVAL(rb_iv_get(self, "@runtime"), value)
);
return self;
View
@@ -4,6 +4,6 @@
extern VALUE cPhubyArray;
void init_phuby_array();
-VALUE Data_Wrap_PhubyArray(zval * value);
+VALUE Data_Wrap_PhubyArray(VALUE rt, zval * value);
#endif
@@ -1,6 +1,6 @@
#include <phuby_conversions.h>
-zval * Phuby_value_to_zval(VALUE value)
+zval * Phuby_value_to_zval(VALUE rt, VALUE value)
{
zval *php_value;
@@ -24,12 +24,19 @@ zval * Phuby_value_to_zval(VALUE value)
case T_STRING:
ZVAL_STRINGL(php_value, StringValuePtr(value), RSTRING_LEN(value), 1);
break;
+ case T_OBJECT:
+ {
+ object_init_ex(php_value, php_ruby_proxy);
+ VALUE map = rb_iv_get(rt, "@proxy_map");
+ rb_hash_aset(map, INT2NUM((int)php_value), value);
+ }
+ break;
}
return php_value;
}
-VALUE Phuby_zval_to_value(zval * value)
+VALUE Phuby_zval_to_value(VALUE rt, zval * value)
{
switch(Z_TYPE_P(value)) {
case IS_NULL:
@@ -51,8 +58,13 @@ VALUE Phuby_zval_to_value(zval * value)
return rb_str_new(Z_STRVAL_P(value), Z_STRLEN_P(value));
break;
case IS_ARRAY:
- return Data_Wrap_PhubyArray(value);
+ return Data_Wrap_PhubyArray(rt, value);
break;
+ case IS_OBJECT:
+ {
+ VALUE map = rb_iv_get(rt, "@proxy_map");
+ return rb_hash_aref(map, INT2NUM((int)value));
+ }
default:
rb_raise(rb_eRuntimeError, "Whoa, I don't know how to convert that");
}
@@ -3,10 +3,10 @@
#include <phuby.h>
-VALUE Phuby_zval_to_value(zval * value);
-zval * Phuby_value_to_zval(VALUE thing);
+VALUE Phuby_zval_to_value(VALUE rt, zval * value);
+zval * Phuby_value_to_zval(VALUE rt, VALUE thing);
-#define ZVAL2VALUE(a) Phuby_zval_to_value(a)
-#define VALUE2ZVAL(a) Phuby_value_to_zval(a)
+#define ZVAL2VALUE(rt, a) Phuby_zval_to_value(rt, a)
+#define VALUE2ZVAL(rt, a) Phuby_value_to_zval(rt, a)
#endif
View
@@ -2,6 +2,36 @@
VALUE cPhubyRuntime;
+PHP_METHOD(RubyProxy, __call)
+{
+ char *function;
+ int function_len;
+ zval *args = NULL;
+
+ if(zend_parse_parameters(ZEND_NUM_ARGS(), "sa|a", &function, &function_len,
+ &args) == FAILURE) {
+ printf("arg################:\n");
+ }
+
+ VALUE rt = rb_funcall(cPhubyRuntime, rb_intern("instance"), 0);
+ VALUE map = rb_iv_get(rt, "@proxy_map");
+ VALUE obj = rb_hash_aref(map, INT2NUM((int)this_ptr));
+
+ rb_funcall(obj, rb_intern(function), 0);
+}
+
+zend_class_entry *php_ruby_proxy;
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_foo___call, 0, 0, 2)
+ ZEND_ARG_INFO(0, function_name)
+ ZEND_ARG_INFO(0, arguments)
+ZEND_END_ARG_INFO()
+
+function_entry php_ruby_functions[] = {
+ PHP_ME(RubyProxy, __call, arginfo_foo___call, 0)
+ { NULL, NULL, NULL }
+};
+
static int phuby_ub_write(const char *str, unsigned int strlen)
{
VALUE self = rb_funcall(cPhubyRuntime, rb_intern("instance"), 0);
@@ -84,6 +114,10 @@ static VALUE start(VALUE self)
php_embed_module.flush = phuby_flush;
php_embed_init(argc, argv);
+ zend_class_entry ce;
+ INIT_CLASS_ENTRY(ce, "RubyProxy", php_ruby_functions);
+
+ php_ruby_proxy = zend_register_internal_class(&ce);
SG(headers_sent) = 0;
SG(request_info).no_headers = 0;
@@ -172,15 +206,15 @@ static VALUE get(VALUE self, VALUE key)
StringValuePtr(key),
RSTRING_LEN(key) + 1,
(void **)&value) == SUCCESS) {
- return ZVAL2VALUE(*value);
+ return ZVAL2VALUE(self, *value);
}
return Qnil;
}
static VALUE set(VALUE self, VALUE key, VALUE value)
{
- zval *php_value = VALUE2ZVAL(value);
+ zval *php_value = VALUE2ZVAL(self, value);
ZEND_SET_SYMBOL(EG(active_symbol_table), StringValuePtr(key), php_value);
@@ -4,6 +4,7 @@
#include <phuby.h>
extern VALUE cPhubyRuntime;
+extern zend_class_entry *php_ruby_proxy;
void init_phuby_runtime();
View
@@ -1,4 +1,6 @@
require 'webrick'
+require 'cgi'
+require 'iconv'
module Phuby
class PHPHandler < WEBrick::HTTPServlet::FileHandler
@@ -20,8 +22,6 @@ def write string
end
def send_headers response_code
- #print "#" * 50
- #puts response_code
end
end
@@ -30,12 +30,16 @@ def initialize server, root = server.config[:DocumentRoot] || Dir.pwd, *args
end
def do_POST req, res
+ req.path << "index.php" if req.path =~ /\/$/
+
return super(req, res) unless req.path =~ /\.php$/
process :POST, req, res
end
def do_GET req, res
+ req.path << "index.php" if req.path =~ /\/$/
+
return super(req, res) unless req.path =~ /\.php$/
process :GET, req, res
@@ -45,7 +49,7 @@ def do_GET req, res
def process verb, req, res
file = File.join(@root, req.path)
- Dir.chdir(@root) do
+ Dir.chdir(File.dirname(file)) do
Phuby::Runtime.php do |rt|
rt.eval("date_default_timezone_set('America/Los_Angeles');")
@@ -58,6 +62,16 @@ def process verb, req, res
rt["_#{verb}"][k] = v
end if :POST == verb
+ # Set CGI server options
+ req.meta_vars.each do |k,v|
+ rt["_SERVER"][k] = v
+ end
+ rt["_SERVER"]['REQUEST_URI'] = req.request_uri.path
+
+ req.cookies.each do |cookie|
+ rt["_COOKIE"][cookie.name] = CGI.unescape(cookie.value)
+ end
+
events = Events.new req, res
rt.with_events(events) do
@@ -67,6 +81,10 @@ def process verb, req, res
end
end
end
+ if res['Location']
+ res['Location'] = CGI.unescape res['Location']
+ res.status = 302
+ end
end
end
end
View
@@ -14,6 +14,7 @@ def self.php &block
def initialize
@events = Events.new
@mutex = Mutex.new
+ @proxy_map = {}
end
def started?
View
@@ -4,13 +4,15 @@ class TestPHPHandler < Phuby::TestCase
FakeServer = Struct.new(:config)
class FakeRequest
- attr_accessor :request_uri, :query, :path
+ attr_accessor :request_uri, :query, :path, :meta_vars, :cookies
def initialize uri
@request_uri = URI.parse "http://localhost#{uri}"
@query = @request_uri.query ? Hash[
*@request_uri.query.split('&').map { |param| param.split('=') }.flatten
] : {}
@path = @request_uri.path
+ @meta_vars = {}
+ @cookies = {}
end
end
View
@@ -12,6 +12,12 @@ def teardown
@rt.stop
end
+ def test_objects_move
+ foo = Object.new
+ @rt['hello'] = foo
+ assert_equal foo, @rt['hello']
+ end
+
def test_eval
@rt.eval("$hi = 'Hello World';")
end
@@ -75,6 +81,22 @@ def test_file_handle
assert_equal 'world', @rt['hi']
end
+ def test_method_call
+ x = Class.new {
+ attr_reader :called
+ def initialize
+ @called = false
+ end
+
+ def hello
+ @called = true
+ end
+ }.new
+ @rt['x'] = x
+ @rt.eval('$x->hello();')
+ assert x.called
+ end
+
#def test_mysql
# @rt['dbhost'] = 'localhost'
# @rt['dbuser'] = 'root'

0 comments on commit 2874870

Please sign in to comment.