Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Attempt to understand and implement mrb_state

  • Loading branch information...
commit 11dca7f0a7d41f478bdbe90bbe1695cb80eb5f85 1 parent b4e8207
@RyanScottLewis authored
Showing with 46 additions and 1 deletion.
  1. +9 −1 examples/sample.lua
  2. +37 −0 lib/lua-mruby.c
View
10 examples/sample.lua
@@ -1,3 +1,6 @@
+-- Note that you must be in the files directory for this to work (i.e. `cd examples`):
+package.cpath = package.cpath .. ";../pkg/?.so"
+
require "mruby"
-- mruby.run([[
@@ -14,4 +17,9 @@ require "mruby"
-- print tak(20, 12, 7)
--
-- ]])
---
+
+mrbState = mrbOpen()
+print( mrbState ) -- Print out the MRuby state
+print( mrbState:stack() ) -- Print out the MRuby state's stack
+mrbClose()
+
View
37 lib/lua-mruby.c
@@ -6,6 +6,9 @@
#include "mruby/proc.h"
#include "mruby/compile.h"
+
+
+
static int lua_mruby_run(lua_State *L) {
mrb_state *mrb = mrb_open();
const char *mrb_code = lua_tostring(L, 1);
@@ -27,7 +30,41 @@ static const struct luaL_reg mrubylib[] = {
{ NULL, NULL }
};
+
+
+
+
+static int lua_mrb_open(lua_State *L) {
+ mrb_state *mrb = mrb_open();
+
+ lua_pushlightuserdata(L, &mrb);
+
+ return 1;
+}
+
+static int lua_mrb_close(lua_State *L) {
+ mrb_state *mrb = lua_topointer(L, 1); // Convert the first argument to a C pointer
+
+ mrb_close(mrb);
+
+ return 1;
+}
+
+static const struct luaL_reg mrblib[] = {
+ { "mrbOpen" , lua_mrb_open },
+ { "mrbClose", lua_mrb_close },
+ { NULL, NULL }
+};
+
+
+
+
+
int luaopen_mruby(lua_State *L) {
+ // luaL_register(L, NULL, mrblib);
+ lua_register(L, "mrbOpen", lua_mrb_open);
+ lua_register(L, "mrbClose", lua_mrb_close);
+
luaL_register(L, "mruby", mrubylib);
return 1;
}
Please sign in to comment.
Something went wrong with that request. Please try again.