Class: CGenerator::MethodPrototype
- Inherits:
-
Prototype
- Object
- Accumulator
- Template
- CFragment
- Prototype
- CGenerator::MethodPrototype
- Defined in:
- lib/cgen/cgen.rb
Defined Under Namespace
Classes: MethodArgumentAccumulator, RbScanArgsSpec
Instance Attribute Summary
Attributes inherited from Accumulator
Instance Method Summary collapse
- #c_array_args(argc_name = 'argc', argv_name = 'argv', &bl) ⇒ Object
- #rb_array_args(args_name = 'args') ⇒ Object
Methods inherited from Prototype
#argc, #initialize, #separator
Methods inherited from Template
Methods inherited from Accumulator
#accept?, #add, #add_one, #add_one_really, #initialize, #inspect, #inspect_one, #output, #output_one, #separator, #to_s
Constructor Details
This class inherits a constructor from CGenerator::Prototype
Instance Method Details
#c_array_args(argc_name = 'argc', argv_name = 'argv', &bl) ⇒ Object
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 |
# File 'lib/cgen/cgen.rb', line 1810 def c_array_args(argc_name = 'argc', argv_name = 'argv', &bl) arguments!.reset arguments!.add_one_really "int #{argc_name}" arguments!.add "*#{argv_name}", "self" @pile.freeze scan_spec = RbScanArgsSpec.new(bl) fmt_str = '"' arg_list = [fmt_str] required = scan_spec.get_required count_required = required ? required.size : 0 if count_required > 0 fmt_str << "#{required.size}" for arg in required arg_list << arg declare arg => "VALUE #{arg}" end end optional = scan_spec.get_optional count_optional = optional ? optional.size : 0 if count_optional > 0 fmt_str << "0" unless count_required > 0 fmt_str << "#{optional.size}" for arg in optional arg_list << arg declare arg => "VALUE #{arg}" end end rest = scan_spec.get_rest if rest fmt_str << "*" arg_list << rest declare rest => "VALUE #{rest}" end block = scan_spec.get_block if block fmt_str << "&" arg_list << block declare block => "VALUE #{block}" end fmt_str << '"' arg_str = arg_list.join ", &" unless arg_str == '""' setup :rb_scan_args => %{ rb_scan_args(#{argc_name}, #{argv_name}, #{arg_str}); }.tabto(0) typecheck = scan_spec.get_typecheck if typecheck for arg, argtype in typecheck next unless argtype and argtype != Object ## this could be a function call setup "#{arg} typecheck" => %{\ if (!NIL_P(#{arg}) && rb_obj_is_kind_of(#{arg}, #{declare_class argtype}) != Qtrue) { VALUE v = rb_funcall( rb_funcall(#{arg}, #{declare_symbol :class}, 0), #{declare_symbol :to_s}, 0); rb_raise(#{declare_class TypeError}, "argument #{arg} declared #{argtype} but passed %s.", StringValueCStr(v)); } }.tabto(0) end end default = scan_spec.get_default if default and default.size > 0 cases = (0..count_optional-1).map { |i| "case #{i}:" + if default[optional[i]] " #{optional[i]} = #{default[optional[i]]};" else "" end }.join("\n") setup :rb_scan_args_defaults => %{\ switch (argc - #{count_required}) {\n#{cases} } }.tabto(0) end end end |
#rb_array_args(args_name = 'args') ⇒ Object
1904 1905 1906 1907 |
# File 'lib/cgen/cgen.rb', line 1904 def rb_array_args args_name = 'args' arguments "#{args_name}" @pile.freeze end |