Skip to content

cahm/mruby-eval_b

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mruby-eval_b / mruby-eval_t

Methods

  • eval_b( str ) - Evaluates the ruby bytecode.
  • eval_t( str ) - Evaluates the ruby script.

Example

class Pet
  def initialize(name)
    @name = name
  end
end

bytecode =
  "\x52\x49\x54\x45\x30\x30\x30\x39\x30\x30\x30\x30\x30\x30\x30\x39" +
  "\x30\x30\x30\x30\x4d\x41\x54\x5a\x20\x20\x20\x20\x30\x30\x30\x39" +
  "\x30\x30\x30\x30\x00\x00\x01\x10\x00\x03\x00\x00\x20\x20\x20\x20" +
  "\x20\x20\x20\x20\xd2\xa7\x00\x00\x00\x3a\x53\x43\x00\x01\x00\x03" +
  "\x00\x02\x46\x9f\x00\x00\x00\x05\x00\x80\x00\x05\x01\x00\x00\x11" +
  "\x00\x80\x40\x43\x00\x80\x00\xc5\x00\x00\x00\x4a\x06\xc2\x00\x00" +
  "\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x50\x65\x74\x00\x03\x43" +
  "\x61\x74\x90\xfe\x00\x00\x00\x36\x53\x43\x00\x01\x00\x02\x00\x02" +
  "\x56\xbe\x00\x00\x00\x05\x00\x80\x00\x48\x01\x00\x02\xc0\x00\x80" +
  "\x00\x46\x00\x00\x00\x05\x00\x00\x00\x29\xf3\x12\x00\x00\x00\x00" +
  "\x00\x00\x00\x00\x00\x01\x00\x04\x6d\x65\x6f\x77\x58\x93\x00\x00" +
  "\x00\x5a\x53\x43\x00\x02\x00\x05\x00\x02\x7f\x09\x00\x00\x00\x09" +
  "\x00\x00\x00\x26\x01\x00\x00\x06\x01\x80\x00\x3d\x02\x00\x00\x8d" +
  "\x01\x81\x00\x3e\x02\x00\x00\xbd\x01\x81\x00\x3e\x01\x00\x00\xa0" +
  "\x01\x00\x00\x29\x5d\x80\x00\x00\x00\x02\x11\x00\x00\x11\x00\x07" +
  "\x3a\x20\x6d\x65\x6f\x77\x21\x83\x3b\x00\x00\x00\x02\x00\x04\x70" +
  "\x75\x74\x73\x00\x05\x40\x6e\x61\x6d\x65\xae\x42\x00\x00\x00\x00"
# ↑: bytecode of ↓
# class Cat < Pet
#   def meow
#     puts "#{@name}: meow!"
#   end
# end
eval_b(bytecode)

eval_t("Momo = Cat.new('momo')")

Momo.meow # => "momo: meow!"

License

under the MIT License:

The difference between eval(CRuby) and eval_t

# eval(CRuby)
puts eval("1+2") # => 3

module A; eval("B = 1"); end
puts A::B # => 1
puts B    # => NameError: uninitialized constant B

eval("return 1") # => LocalJumpError: unexpected return

# eval_t

puts eval_t("1+2") # => nil

module A; eval_t("B = 1"); end
puts A::B # => 1
puts B    # => 1

eval_t("return 1") # => 1

日本語による説明

といっても、masamitsu-murase さんの mruby_require_sample をパクっただけなので説明も何もないのですが。

src/mrb_eval_b.c の「 // #define DISABLE_EVAL_T 」をコメント解除すると、eval_t は作成されず eval_b のみになります。

include/opcode.h は、使用する mruby の src/opcode.h に入れ替えた方が良いでしょう。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors