node/lib/metasm/samples/dynamic_ruby.rb
def compile_args_m1(func, args)
		c = C::Variable.new("arg_c", C::BaseType.new(:int, :unsigned))
		v = C::Variable.new("arg_v", C::Pointer.new(value))
		@scope.symbol[c.name] = c
		@scope.symbol[v.name] = v
		func.type.args.unshift v
		func.type.args.unshift c

		args[1].times { |i|
			local(i+2, C::CExpression[v, :'[]', [i]])
		}

		if args[2]
			# [:block, [:lasgn, 2, [:lit, 4]]]
			raise Fail, "unhandled vararglist #{args.inspect}" if args[2][0] != :block
			args[2][1..-1].each_with_index { |a, i|
				raise Fail, "unhandled arg #{a.inspect}" if a[0] != :lasgn
				cnd = C::CExpression[c, :>, i]
				thn = C::CExpression[local(a[1], :none), :'=', [v, :'[]', [i]]]
				els = C::Block.new(@scope)
				ast_to_c(a, els, false)
				@scope.statements << C::If.new(cnd, thn, els)
			}
		end

		if args[3]
			raise Fail, "unhandled vararglist3 #{args.inspect}" if args[3][0] != :lasgn
			skiplen = args[1] + args[2].length - 1
			alloc = fcall('rb_ary_new4', [c, :-, [skiplen]], [v, :+, [skiplen]])
			local(args[3][1], C::CExpression[[c, :>, skiplen], :'?:', [alloc, fcall('rb_ary_new')]])
		end
	end
# update func prototype to reflect arity -1
# VALUE func(int argc, VALUE *argv, VALUE self)
update func prototype to reflect arity -1
VALUE func(int argc, VALUE *argv, VALUE self)
update func prototype to reflect arity - 1 VALUE func ( int argc VALUE * argv VALUE self )
def compile_args_m1(func, args)
		c = C::Variable.new("arg_c", C::BaseType.new(:int, :unsigned))
		v = C::Variable.new("arg_v", C::Pointer.new(value))
		@scope.symbol[c.name] = c
		@scope.symbol[v.name] = v
		func.type.args.unshift v
		func.type.args.unshift c
		args[1].times { |i|
			local(i+2, C::CExpression[v, :'[]', [i]])
		}
		if args[2]
			raise Fail, "unhandled vararglist #{args.inspect}" if args[2][0] != :block
			args[2][1..-1].each_with_index { |a, i|
				raise Fail, "unhandled arg #{a.inspect}" if a[0] != :lasgn
				cnd = C::CExpression[c, :>, i]
				thn = C::CExpression[local(a[1], :none), :'=', [v, :'[]', [i]]]
				els = C::Block.new(@scope)
				ast_to_c(a, els, false)
				@scope.statements << C::If.new(cnd, thn, els)
			}
		end
		if args[3]
			raise Fail, "unhandled vararglist3 #{args.inspect}" if args[3][0] != :lasgn
			skiplen = args[1] + args[2].length - 1
			alloc = fcall('rb_ary_new4', [c, :-, [skiplen]], [v, :+, [skiplen]])
			local(args[3][1], C::CExpression[[c, :>, skiplen], :'?:', [alloc, fcall('rb_ary_new')]])
		end
	end
def compile_args_m1 ( func , args ) c = C :: Variable . new ( "arg_c" , C :: BaseType . new ( :int , :unsigned ) ) v = C :: Variable . new ( "arg_v" , C :: Pointer . new ( value ) ) @scope . symbol [ c . name ] = c @scope . symbol [ v . name ] = v func . type . args . unshift v func . type . args . unshift c args [ 1 ] . times { | i | local ( i + 2 , C :: CExpression [ v , :' [] ' , [ i ] ] ) } if args [ 2 ] raise Fail , "unhandled vararglist #{args.inspect}" if args [ 2 ] [ 0 ] != :block args [ 2 ] [ 1 .. - 1 ] . each_with_index { | a , i | raise Fail , "unhandled arg #{a.inspect}" if a [ 0 ] != :lasgn cnd = C :: CExpression [ c , :> , i ] thn = C :: CExpression [ local ( a [ 1 ] , :none ) , :' = ' , [ v , :' [] ' , [ i ] ] ] els = C :: Block . new ( @scope ) ast_to_c ( a , els , false ) @scope . statements << C :: If . new ( cnd , thn , els ) } end if args [ 3 ] raise Fail , "unhandled vararglist3 #{args.inspect}" if args [ 3 ] [ 0 ] != :lasgn skiplen = args [ 1 ] + args [ 2 ] . length - 1 alloc = fcall ( 'rb_ary_new4' , [ c , :- , [ skiplen ] ] , [ v , :+ , [ skiplen ] ] ) local ( args [ 3 ] [ 1 ] , C :: CExpression [ [ c , :> , skiplen ] , :' ?: ' , [ alloc , fcall ( 'rb_ary_new' ) ] ] ) end end
update func prototype to reflect arity -1
VALUE func(int argc, VALUE *argv, VALUE self)
update func prototype to reflect arity - 1 VALUE func ( int argc VALUE * argv VALUE self )
# [:block, [:lasgn, 2, [:lit, 4]]]
func
args