Created
March 27, 2017 06:45
-
-
Save yannick/cb38f12089b95e7293dafda17fafa852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.stdio; | |
| import mruby; | |
| import mruby.compile; | |
| import mruby.value; | |
| import mruby.data; | |
| import mruby.mrb_class; | |
| import mruby.value; | |
| import std.string; | |
| import std.conv; | |
| struct Test | |
| { | |
| string mystr; | |
| ulong myulong; | |
| @property string[] mystrarr; | |
| string[string] mystrmap; | |
| } | |
| Test t; | |
| extern(C) mrb_value mrb_nil_value(); | |
| extern(C) { | |
| mrb_value getval(mrb_state *mrb, mrb_value self) | |
| { | |
| char* key; | |
| string k; | |
| mrb_get_args(mrb, "z", &key); | |
| // | |
| //writeln("key: ", key.fromStringz); | |
| k = key.fromStringz.to!string; | |
| string val = t.mystrmap.get(k,null); | |
| if (val) | |
| return mrb_str_new(mrb, val.toStringz, val.length); | |
| else | |
| return self; | |
| //return self; | |
| } | |
| } | |
| import std.datetime; | |
| import core.thread; | |
| void main() | |
| { | |
| t.mystrmap["x"] = "xYOLO"; | |
| auto mrb = mrb_open; | |
| auto Foo = mrb_define_class(mrb, "Foo", mrb.object_class); | |
| mrb_define_method(mrb, Foo, "get", &getval, 262144); | |
| auto code = "x = Foo.new; puts 'hello'; puts x.get('x') "; | |
| writeln("Executing Ruby code from D!"); | |
| // StopWatch sw; | |
| // sw.start(); | |
| mrb_load_string(mrb, code.toStringz); | |
| // sw.stop(); | |
| // writeln( sw.peek().msecs, " [ms] per call:", sw.peek().msecs.to!float / 50_000_000 ); | |
| // writeln( "per second: ", 50_000_000 /sw.peek().seconds.to!float); | |
| // writeln("finished"); | |
| // | |
| // | |
| // Thread.sleep( dur!("seconds")( 1 ) ); | |
| // char[] buf; | |
| // stdin.readln(buf); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name "test-mruby" | |
| description "A minimal D application." | |
| copyright "Copyright © 2015, yannick" | |
| authors "yannick" | |
| dependency "d_mruby" version="~>0.0.2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment