Created
March 2, 2017 16:26
-
-
Save shibafu528/9897cf84331682051ae2f460869563ba to your computer and use it in GitHub Desktop.
mruby、__printstr__を上書きしたいんだけどなんかうまく出力されてくれない気がする。昔はできたはず。
This file contains 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
// -std=c99 -lmruby -lm | |
// using default gembox | |
#include <stdio.h> | |
#include <mruby.h> | |
#include <mruby/compile.h> | |
static mrb_value mrb_printstr(mrb_state *mrb, mrb_value self) { | |
mrb_value argv; | |
mrb_get_args(mrb, "o", &argv); | |
if (mrb_string_p(argv)) { | |
char *cstr = mrb_str_to_cstr(mrb, argv); | |
printf("(user function) %s\n", cstr); | |
} | |
return argv; | |
} | |
int main(int argc, char* argv[]) { | |
mrb_state *mrb = mrb_open(); | |
// Expected stdout: | |
// Hello | |
// Actual stdout: | |
// Hello | |
mrb_load_string(mrb, "puts 'Hello'"); | |
struct RClass* krn = mrb->kernel_module; | |
mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, MRB_ARGS_REQ(1)); | |
// Excepted stdout: | |
// (user function) Bye | |
// Actual stdout: | |
// Bye | |
mrb_load_string(mrb, "puts 'Bye'"); | |
mrb_close(mrb); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment