Skip to content
Browse files

init

  • Loading branch information...
0 parents commit 8d34941635a5252c2c2ab1093d52e465c67c7f2d @RyanScottLewis committed Dec 18, 2012
Showing with 171 additions and 0 deletions.
  1. +15 −0 Makefile
  2. +105 −0 README.md
  3. +17 −0 examples/sample.lua
  4. +34 −0 lib/lua-mruby.c
15 Makefile
@@ -0,0 +1,15 @@
+MRUBY_ROOT = /Users/ryguy/Code/C/Source/mruby
+LUA_ROOT = /usr/local/Cellar/lua/5.1.4
+
+INCLUDES = -I$(MRUBY_ROOT)/include -I$(MRUBY_ROOT)/src -I$(LUA_ROOT)/include
+LIB = -lm $(MRUBY_ROOT)/mrblib/mrblib.o -lm $(MRUBY_ROOT)/lib/libmruby.a
+CFLAGS = $(INCLUDES) -llua -O3 -g -Wall -Werror-implicit-function-declaration -shared -O
+
+CC = gcc
+
+all :
+ mkdir pkg
+ $(CC) $(CFLAGS) -o pkg/mruby.so lib/lua-mruby.c $(LIB)
+
+clean :
+ rm -rf pkg
105 README.md
@@ -0,0 +1,105 @@
+# lua-mruby
+
+Use [mruby][mruby] from within [lua][lua].
+
+## Install
+
+```shell
+$ make
+```
+
+## Usage
+
+```lua
+require "mruby"
+
+mruby.run([[
+
+ class User
+
+ class Error < StandardError; end
+
+ class InvalidOptionsError < Error
+ def to_s
+ 'The options argument must be a Hash or respond to :to_hash or :to_h'
+ end
+ end
+
+ class InvalidNameError < Error
+ def to_s
+ 'The :name option must not be nil and must be a String or respond to :to_s'
+ end
+ end
+
+ class InvalidAgeError < Error
+ def to_s
+ 'The :age option must be an Integer or respond to :to_i'
+ end
+ end
+
+ def initialize(options)
+ raise InvalidOptionsError unless options.is_a?(Hash) || options.respond_to?(:to_hash) || options.respond_to?(:to_h)
+ options = options.to_hash rescue options.to_h unless options.is_a?(Hash)
+
+ raise InvalidNameError unless !options[:name].nil? || options[:name].is_a?(String) || options[:name].respond_to?(:to_s)
+ @name = options[:name].to_s
+
+ raise InvalidAgeError unless options[:age].nil? || options[:age].is_a?(Integer) || options[:age].respond_to?(:to_i)
+ @age = options[:age].to_i
+ end
+
+ end
+
+]])
+```
+
+## Copyright
+
+Copyright © 2012 Ryan Scott Lewis <ryan@rynet.us>.
+
+The MIT License (MIT) - See LICENSE for further details.
+
+[message_queue]: http://en.wikipedia.org/wiki/Message_queue
+[queue]: http://rubydoc.info/stdlib/thread/Queue
+[service]: https://github.com/RyanScottLewis/service
+
+
+
+
+
+
+
+
+
+
+* Install
+
+ make
+
+* Example
+
+
+
+* Test
+
+ ```lua
+ require "mruby"
+
+ file = "./test.mrb"
+
+ print(">> mruby file run ./test.mrb")
+ mruby.run_file(file)
+
+ print(">> mruby code run")
+ mruby.run("puts 'mruby code run on Lua!!'")
+ ```
+
+* Run
+
+ lua sample.lua
+ lua mruby_on.lua
+
+ >> mruby file run ./test.mrb
+ mruby file run on Lua!!
+ >> mruby code run
+ mruby code run on Lua!!
17 examples/sample.lua
@@ -0,0 +1,17 @@
+require "mruby"
+
+-- mruby.run([[
+--
+-- def tak(x, y, z)
+--
+-- if y >= x
+-- z
+-- else
+-- tak(tak(x - 1, y, z),tak(y - 1, z, x),tak(z - 1, x, y))
+-- end
+-- end
+--
+-- print tak(20, 12, 7)
+--
+-- ]])
+--
34 lib/lua-mruby.c
@@ -0,0 +1,34 @@
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+
+#include "mruby.h"
+#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);
+ // struct mrb_parser_state* parser = mrb_parse_string(mrb, mrb_code, NULL);
+ // int mrb_generated_code = mrb_generate_code(mrb, parser->tree);
+ //
+ // mrb_run( mrb, mrb_proc_new(mrb, mrb->irep[mrb_generated_code]), mrb_nil_value() );
+ //
+ // int lua_return = mrb_generated_code > 0 ? 0 : 1;
+ // lua_pushnumber(L, lua_return);
+ //
+ // mrb_close(mrb);
+
+ return 1;
+}
+
+static const struct luaL_reg mrubylib[] = {
+ { "run", lua_mruby_run },
+ { NULL, NULL }
+};
+
+int luaopen_mruby(lua_State *L) {
+ // luaL_openlib(L, "mruby", mrubylib, 0);
+ luaL_register(L, "mruby", mrubylib);
+ return 1;
+}

0 comments on commit 8d34941

Please sign in to comment.
Something went wrong with that request. Please try again.