portable_mruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +195 -0
- data/exe/portable-mruby +6 -0
- data/lib/portable_mruby/binary_manager.rb +225 -0
- data/lib/portable_mruby/builder.rb +97 -0
- data/lib/portable_mruby/bytecode_compiler.rb +19 -0
- data/lib/portable_mruby/c_generator.rb +94 -0
- data/lib/portable_mruby/cli.rb +136 -0
- data/lib/portable_mruby/version.rb +6 -0
- data/lib/portable_mruby.rb +15 -0
- data/vendor/mruby/bin/mrbc.com +0 -0
- data/vendor/mruby/include/mrbconf.h +230 -0
- data/vendor/mruby/include/mruby/array.h +303 -0
- data/vendor/mruby/include/mruby/boxing_nan.h +169 -0
- data/vendor/mruby/include/mruby/boxing_no.h +59 -0
- data/vendor/mruby/include/mruby/boxing_word.h +251 -0
- data/vendor/mruby/include/mruby/class.h +104 -0
- data/vendor/mruby/include/mruby/common.h +118 -0
- data/vendor/mruby/include/mruby/compile.h +185 -0
- data/vendor/mruby/include/mruby/data.h +76 -0
- data/vendor/mruby/include/mruby/debug.h +75 -0
- data/vendor/mruby/include/mruby/dump.h +159 -0
- data/vendor/mruby/include/mruby/endian.h +44 -0
- data/vendor/mruby/include/mruby/error.h +132 -0
- data/vendor/mruby/include/mruby/gc.h +72 -0
- data/vendor/mruby/include/mruby/gems/mruby-dir/include/dir_hal.h +79 -0
- data/vendor/mruby/include/mruby/gems/mruby-io/include/io_hal.h +451 -0
- data/vendor/mruby/include/mruby/gems/mruby-io/include/mruby/ext/io.h +76 -0
- data/vendor/mruby/include/mruby/gems/mruby-socket/include/socket_hal.h +83 -0
- data/vendor/mruby/include/mruby/gems/mruby-time/include/mruby/time.h +27 -0
- data/vendor/mruby/include/mruby/hash.h +234 -0
- data/vendor/mruby/include/mruby/internal.h +274 -0
- data/vendor/mruby/include/mruby/irep.h +142 -0
- data/vendor/mruby/include/mruby/istruct.h +50 -0
- data/vendor/mruby/include/mruby/khash.h +455 -0
- data/vendor/mruby/include/mruby/mempool.h +19 -0
- data/vendor/mruby/include/mruby/numeric.h +174 -0
- data/vendor/mruby/include/mruby/object.h +45 -0
- data/vendor/mruby/include/mruby/opcode.h +69 -0
- data/vendor/mruby/include/mruby/ops.h +120 -0
- data/vendor/mruby/include/mruby/presym/disable.h +72 -0
- data/vendor/mruby/include/mruby/presym/enable.h +39 -0
- data/vendor/mruby/include/mruby/presym/id.h +1423 -0
- data/vendor/mruby/include/mruby/presym/scanning.h +81 -0
- data/vendor/mruby/include/mruby/presym/table.h +2847 -0
- data/vendor/mruby/include/mruby/presym.h +41 -0
- data/vendor/mruby/include/mruby/proc.h +186 -0
- data/vendor/mruby/include/mruby/range.h +77 -0
- data/vendor/mruby/include/mruby/re.h +16 -0
- data/vendor/mruby/include/mruby/string.h +428 -0
- data/vendor/mruby/include/mruby/throw.h +57 -0
- data/vendor/mruby/include/mruby/value.h +471 -0
- data/vendor/mruby/include/mruby/variable.h +108 -0
- data/vendor/mruby/include/mruby/version.h +143 -0
- data/vendor/mruby/include/mruby.h +1614 -0
- data/vendor/mruby/lib/libmruby.a +0 -0
- metadata +102 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "optparse"
|
|
4
|
+
|
|
5
|
+
module PortableMruby
|
|
6
|
+
class CLI
|
|
7
|
+
def self.run(args = ARGV)
|
|
8
|
+
new(args).run
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize(args)
|
|
12
|
+
@args = args.dup
|
|
13
|
+
@options = {
|
|
14
|
+
entry: nil,
|
|
15
|
+
dir: ".",
|
|
16
|
+
output: "app.com",
|
|
17
|
+
verbose: false,
|
|
18
|
+
mruby_source: nil
|
|
19
|
+
}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def run
|
|
23
|
+
command = parse_command
|
|
24
|
+
|
|
25
|
+
case command
|
|
26
|
+
when "build"
|
|
27
|
+
build
|
|
28
|
+
when "version"
|
|
29
|
+
version
|
|
30
|
+
when "help", nil
|
|
31
|
+
help
|
|
32
|
+
else
|
|
33
|
+
$stderr.puts "Unknown command: #{command}"
|
|
34
|
+
$stderr.puts "Run 'portable-mruby help' for usage"
|
|
35
|
+
exit 1
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def parse_command
|
|
42
|
+
# Extract command (first non-option argument)
|
|
43
|
+
idx = @args.index { |arg| !arg.start_with?("-") }
|
|
44
|
+
idx ? @args.delete_at(idx) : nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def build
|
|
48
|
+
parser = OptionParser.new do |opts|
|
|
49
|
+
opts.banner = "Usage: portable-mruby build [options]"
|
|
50
|
+
opts.separator ""
|
|
51
|
+
opts.separator "Options:"
|
|
52
|
+
|
|
53
|
+
opts.on("-e", "--entry FILE", "Entry point Ruby file (runs last)") do |f|
|
|
54
|
+
@options[:entry] = f
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
opts.on("-d", "--dir DIR", "Source directory (default: .)") do |d|
|
|
58
|
+
@options[:dir] = d
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
opts.on("-o", "--output FILE", "Output binary name (default: app.com)") do |o|
|
|
62
|
+
@options[:output] = o
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
opts.on("--mruby-source DIR", "Build mruby from source directory instead of using bundled") do |d|
|
|
66
|
+
@options[:mruby_source] = d
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
opts.on("-v", "--verbose", "Verbose output") do
|
|
70
|
+
@options[:verbose] = true
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
opts.on("-h", "--help", "Show this help") do
|
|
74
|
+
puts opts
|
|
75
|
+
exit
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
parser.parse!(@args)
|
|
80
|
+
|
|
81
|
+
builder = Builder.new(
|
|
82
|
+
entry_file: @options[:entry],
|
|
83
|
+
source_dir: @options[:dir],
|
|
84
|
+
output: @options[:output],
|
|
85
|
+
verbose: @options[:verbose],
|
|
86
|
+
mruby_source: @options[:mruby_source]
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
builder.build
|
|
90
|
+
puts "Built: #{@options[:output]}"
|
|
91
|
+
rescue Error => e
|
|
92
|
+
$stderr.puts "Error: #{e.message}"
|
|
93
|
+
exit 1
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def version
|
|
97
|
+
puts "portable-mruby #{VERSION}"
|
|
98
|
+
puts "mruby #{MRUBY_VERSION}"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def help
|
|
102
|
+
puts <<~HELP
|
|
103
|
+
portable-mruby - Build portable Ruby executables
|
|
104
|
+
|
|
105
|
+
Usage:
|
|
106
|
+
portable-mruby <command> [options]
|
|
107
|
+
|
|
108
|
+
Commands:
|
|
109
|
+
build Build a portable executable from Ruby source
|
|
110
|
+
version Show version information
|
|
111
|
+
help Show this help message
|
|
112
|
+
|
|
113
|
+
Build Options:
|
|
114
|
+
-e, --entry FILE Entry point Ruby file (runs last)
|
|
115
|
+
-d, --dir DIR Source directory (default: .)
|
|
116
|
+
-o, --output FILE Output binary name (default: app.com)
|
|
117
|
+
--mruby-source DIR Build mruby from source directory
|
|
118
|
+
-v, --verbose Verbose output
|
|
119
|
+
|
|
120
|
+
Environment Variables:
|
|
121
|
+
COSMO_ROOT Path to cosmocc toolchain (auto-downloaded if not set)
|
|
122
|
+
|
|
123
|
+
Examples:
|
|
124
|
+
portable-mruby build -d src/ -o myapp.com
|
|
125
|
+
portable-mruby build -e main.rb -d src/ -o myapp.com
|
|
126
|
+
portable-mruby build -e main.rb --mruby-source ~/mruby
|
|
127
|
+
|
|
128
|
+
All .rb files in the directory are compiled and executed in sorted order.
|
|
129
|
+
If --entry is specified, that file runs last (after all others).
|
|
130
|
+
|
|
131
|
+
The resulting binary runs on Linux, macOS, Windows, FreeBSD, OpenBSD, and NetBSD
|
|
132
|
+
for both x86_64 and ARM64 architectures.
|
|
133
|
+
HELP
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "portable_mruby/version"
|
|
4
|
+
|
|
5
|
+
module PortableMruby
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
class BuildError < Error; end
|
|
8
|
+
class CompileError < Error; end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
require_relative "portable_mruby/binary_manager"
|
|
12
|
+
require_relative "portable_mruby/bytecode_compiler"
|
|
13
|
+
require_relative "portable_mruby/c_generator"
|
|
14
|
+
require_relative "portable_mruby/builder"
|
|
15
|
+
require_relative "portable_mruby/cli"
|
|
Binary file
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** mrbconf.h - mruby core configuration
|
|
3
|
+
**
|
|
4
|
+
** See Copyright Notice in mruby.h
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
#ifndef MRUBYCONF_H
|
|
8
|
+
#define MRUBYCONF_H
|
|
9
|
+
|
|
10
|
+
/* architecture selection: */
|
|
11
|
+
/* specify -DMRB_32BIT or -DMRB_64BIT to override */
|
|
12
|
+
#if !defined(MRB_32BIT) && !defined(MRB_64BIT)
|
|
13
|
+
#if UINT64_MAX == SIZE_MAX
|
|
14
|
+
#define MRB_64BIT
|
|
15
|
+
#else
|
|
16
|
+
#define MRB_32BIT
|
|
17
|
+
#endif
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
#if defined(MRB_32BIT) && defined(MRB_64BIT)
|
|
21
|
+
#error Cannot build for 32 and 64-bit architecture at the same time
|
|
22
|
+
#endif
|
|
23
|
+
|
|
24
|
+
/* configuration options: */
|
|
25
|
+
/* add -DMRB_USE_FLOAT32 to use float instead of double for floating-point numbers */
|
|
26
|
+
//#define MRB_USE_FLOAT32
|
|
27
|
+
|
|
28
|
+
/* exclude floating-point numbers */
|
|
29
|
+
//#define MRB_NO_FLOAT
|
|
30
|
+
|
|
31
|
+
/* obsolete configuration */
|
|
32
|
+
#if defined(MRB_USE_FLOAT)
|
|
33
|
+
# define MRB_USE_FLOAT32
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
/* obsolete configuration */
|
|
37
|
+
#if defined(MRB_WITHOUT_FLOAT)
|
|
38
|
+
# define MRB_NO_FLOAT
|
|
39
|
+
#endif
|
|
40
|
+
|
|
41
|
+
#if defined(MRB_USE_FLOAT32) && defined(MRB_NO_FLOAT)
|
|
42
|
+
#error Cannot define MRB_USE_FLOAT32 and MRB_NO_FLOAT at the same time
|
|
43
|
+
#endif
|
|
44
|
+
|
|
45
|
+
/* define on big endian machines; used by MRB_NAN_BOXING, etc. */
|
|
46
|
+
#ifndef MRB_ENDIAN_BIG
|
|
47
|
+
# if (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \
|
|
48
|
+
(defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
|
49
|
+
# define MRB_ENDIAN_BIG
|
|
50
|
+
# endif
|
|
51
|
+
#endif
|
|
52
|
+
|
|
53
|
+
/* represent mrb_value in boxed double; conflict with MRB_USE_FLOAT32 and MRB_NO_FLOAT */
|
|
54
|
+
//#define MRB_NAN_BOXING
|
|
55
|
+
|
|
56
|
+
/* represent mrb_value as a word (natural unit of data for the processor) */
|
|
57
|
+
//#define MRB_WORD_BOXING
|
|
58
|
+
|
|
59
|
+
/* represent mrb_value as a struct; occupies 2 words */
|
|
60
|
+
//#define MRB_NO_BOXING
|
|
61
|
+
|
|
62
|
+
/* if no specific boxing type is chosen */
|
|
63
|
+
#if !defined(MRB_NAN_BOXING) && !defined(MRB_WORD_BOXING) && !defined(MRB_NO_BOXING)
|
|
64
|
+
# define MRB_WORD_BOXING
|
|
65
|
+
#endif
|
|
66
|
+
|
|
67
|
+
/* if defined mruby allocates Float objects in the heap to keep full precision if needed */
|
|
68
|
+
//#define MRB_WORDBOX_NO_FLOAT_TRUNCATE
|
|
69
|
+
|
|
70
|
+
/* add -DMRB_INT32 to use 32-bit integer for mrb_int; conflict with MRB_INT64;
|
|
71
|
+
Default for 32-bit CPU mode. */
|
|
72
|
+
//#define MRB_INT32
|
|
73
|
+
|
|
74
|
+
/* add -DMRB_INT64 to use 64-bit integer for mrb_int; conflict with MRB_INT32;
|
|
75
|
+
Default for 64-bit CPU mode (unless using MRB_NAN_BOXING). */
|
|
76
|
+
//#define MRB_INT64
|
|
77
|
+
|
|
78
|
+
/* if no specific integer type is chosen */
|
|
79
|
+
#if !defined(MRB_INT32) && !defined(MRB_INT64)
|
|
80
|
+
# if defined(MRB_64BIT) && !defined(MRB_NAN_BOXING)
|
|
81
|
+
/* Use 64-bit integers on 64-bit architecture (without MRB_NAN_BOXING) */
|
|
82
|
+
# define MRB_INT64
|
|
83
|
+
# else
|
|
84
|
+
/* Otherwise use 32-bit integers */
|
|
85
|
+
# define MRB_INT32
|
|
86
|
+
# endif
|
|
87
|
+
#endif
|
|
88
|
+
|
|
89
|
+
/* call malloc_trim(0) from mrb_full_gc() */
|
|
90
|
+
//#define MRB_USE_MALLOC_TRIM
|
|
91
|
+
|
|
92
|
+
/* string class to handle UTF-8 encoding */
|
|
93
|
+
//#define MRB_UTF8_STRING
|
|
94
|
+
|
|
95
|
+
/* maximum length of strings */
|
|
96
|
+
/* the default value is 1MB */
|
|
97
|
+
/* set this value to zero to skip the check */
|
|
98
|
+
//#define MRB_STR_LENGTH_MAX 1048576
|
|
99
|
+
|
|
100
|
+
/* maximum length of arrays */
|
|
101
|
+
/* the default value is 2**17 entries */
|
|
102
|
+
/* set this value to zero to skip the check */
|
|
103
|
+
//#define MRB_ARY_LENGTH_MAX 131072
|
|
104
|
+
|
|
105
|
+
/* argv max size in mrb_funcall */
|
|
106
|
+
//#define MRB_FUNCALL_ARGC_MAX 16
|
|
107
|
+
|
|
108
|
+
/* number of object per heap page */
|
|
109
|
+
//#define MRB_HEAP_PAGE_SIZE 1024
|
|
110
|
+
|
|
111
|
+
/* define if your platform does not support etext, edata */
|
|
112
|
+
//#define MRB_NO_DEFAULT_RO_DATA_P
|
|
113
|
+
|
|
114
|
+
/* define if your platform supports etext, edata */
|
|
115
|
+
//#define MRB_USE_RO_DATA_P_ETEXT
|
|
116
|
+
/* use MRB_USE_ETEXT_RO_DATA_P by default on Linux */
|
|
117
|
+
#if (defined(__linux__) && !defined(__KERNEL__))
|
|
118
|
+
#define MRB_USE_ETEXT_RO_DATA_P
|
|
119
|
+
#endif
|
|
120
|
+
|
|
121
|
+
/* you can provide and use mrb_ro_data_p() for your platform.
|
|
122
|
+
prototype is `mrb_bool mrb_ro_data_p(const char *ptr)` */
|
|
123
|
+
//#define MRB_USE_CUSTOM_RO_DATA_P
|
|
124
|
+
|
|
125
|
+
/* turn off generational GC by default */
|
|
126
|
+
//#define MRB_GC_TURN_OFF_GENERATIONAL
|
|
127
|
+
|
|
128
|
+
/* default size of khash table bucket */
|
|
129
|
+
//#define KHASH_DEFAULT_SIZE 32
|
|
130
|
+
|
|
131
|
+
/* allocated memory address alignment */
|
|
132
|
+
//#define POOL_ALIGNMENT 4
|
|
133
|
+
|
|
134
|
+
/* page size of memory pool */
|
|
135
|
+
//#define POOL_PAGE_SIZE 16000
|
|
136
|
+
|
|
137
|
+
/* arena size */
|
|
138
|
+
//#define MRB_GC_ARENA_SIZE 100
|
|
139
|
+
|
|
140
|
+
/* fixed size GC arena */
|
|
141
|
+
//#define MRB_GC_FIXED_ARENA
|
|
142
|
+
|
|
143
|
+
/* state atexit stack size */
|
|
144
|
+
//#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5
|
|
145
|
+
|
|
146
|
+
/* fixed size state atexit stack */
|
|
147
|
+
//#define MRB_FIXED_STATE_ATEXIT_STACK
|
|
148
|
+
|
|
149
|
+
/* -DMRB_NO_XXXX to drop following features */
|
|
150
|
+
//#define MRB_NO_STDIO /* use of stdio */
|
|
151
|
+
|
|
152
|
+
/* -DMRB_USE_XXXX to enable following features */
|
|
153
|
+
//#define MRB_USE_DEBUG_HOOK /* hooks for debugger */
|
|
154
|
+
//#define MRB_USE_ALL_SYMBOLS /* Symbol.all_symbols */
|
|
155
|
+
|
|
156
|
+
/* Symbol table configuration */
|
|
157
|
+
/* Threshold for switching from linear search to hash table */
|
|
158
|
+
#ifndef MRB_SYMBOL_LINEAR_THRESHOLD
|
|
159
|
+
#define MRB_SYMBOL_LINEAR_THRESHOLD 256
|
|
160
|
+
#endif
|
|
161
|
+
|
|
162
|
+
/* obsolete configurations */
|
|
163
|
+
#if defined(DISABLE_STDIO) || defined(MRB_DISABLE_STDIO)
|
|
164
|
+
# define MRB_NO_STDIO
|
|
165
|
+
#endif
|
|
166
|
+
#if defined(MRB_DISABLE_DIRECT_THREADING) || defined(MRB_NO_DIRECT_THREADING)
|
|
167
|
+
# define MRB_USE_VM_SWITCH_DISPATCH
|
|
168
|
+
#endif
|
|
169
|
+
#if defined(ENABLE_DEBUG) || defined(MRB_ENABLE_DEBUG_HOOK)
|
|
170
|
+
# define MRB_USE_DEBUG_HOOK
|
|
171
|
+
#endif
|
|
172
|
+
#ifdef MRB_ENABLE_ALL_SYMBOLS
|
|
173
|
+
# define MRB_USE_ALL_SYMBOLS
|
|
174
|
+
#endif
|
|
175
|
+
#ifdef MRB_ENABLE_CXX_ABI
|
|
176
|
+
# define MRB_USE_CXX_ABI
|
|
177
|
+
#endif
|
|
178
|
+
#ifdef MRB_ENABLE_CXX_EXCEPTION
|
|
179
|
+
# define MRB_USE_CXX_EXCEPTION
|
|
180
|
+
#endif
|
|
181
|
+
|
|
182
|
+
/* end of configuration */
|
|
183
|
+
|
|
184
|
+
#ifndef MRB_NO_STDIO
|
|
185
|
+
# include <stdio.h>
|
|
186
|
+
#endif
|
|
187
|
+
|
|
188
|
+
/*
|
|
189
|
+
** mruby tuning profiles
|
|
190
|
+
**/
|
|
191
|
+
|
|
192
|
+
/* A profile for micro controllers */
|
|
193
|
+
#if defined(MRB_CONSTRAINED_BASELINE_PROFILE)
|
|
194
|
+
# ifndef MRB_NO_METHOD_CACHE
|
|
195
|
+
# define MRB_NO_METHOD_CACHE
|
|
196
|
+
# endif
|
|
197
|
+
|
|
198
|
+
# ifndef KHASH_DEFAULT_SIZE
|
|
199
|
+
# define KHASH_DEFAULT_SIZE 16
|
|
200
|
+
# endif
|
|
201
|
+
|
|
202
|
+
# ifndef MRB_HEAP_PAGE_SIZE
|
|
203
|
+
# define MRB_HEAP_PAGE_SIZE 256
|
|
204
|
+
# endif
|
|
205
|
+
|
|
206
|
+
/* A profile for default mruby */
|
|
207
|
+
#elif defined(MRB_BASELINE_PROFILE)
|
|
208
|
+
|
|
209
|
+
/* A profile for desktop computers or workstations; rich memory! */
|
|
210
|
+
#elif defined(MRB_MAIN_PROFILE)
|
|
211
|
+
# ifndef MRB_METHOD_CACHE_SIZE
|
|
212
|
+
# define MRB_METHOD_CACHE_SIZE (1<<10)
|
|
213
|
+
# endif
|
|
214
|
+
|
|
215
|
+
# ifndef MRB_HEAP_PAGE_SIZE
|
|
216
|
+
# define MRB_HEAP_PAGE_SIZE 4096
|
|
217
|
+
# endif
|
|
218
|
+
|
|
219
|
+
/* A profile for server; mruby vm is long life */
|
|
220
|
+
#elif defined(MRB_HIGH_PROFILE)
|
|
221
|
+
# ifndef MRB_METHOD_CACHE_SIZE
|
|
222
|
+
# define MRB_METHOD_CACHE_SIZE (1<<12)
|
|
223
|
+
# endif
|
|
224
|
+
|
|
225
|
+
# ifndef MRB_HEAP_PAGE_SIZE
|
|
226
|
+
# define MRB_HEAP_PAGE_SIZE 4096
|
|
227
|
+
# endif
|
|
228
|
+
#endif
|
|
229
|
+
|
|
230
|
+
#endif /* MRUBYCONF_H */
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
** @file mruby/array.h - Array class
|
|
3
|
+
**
|
|
4
|
+
** See Copyright Notice in mruby.h
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
#ifndef MRUBY_ARRAY_H
|
|
8
|
+
#define MRUBY_ARRAY_H
|
|
9
|
+
|
|
10
|
+
#include "common.h"
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Array class
|
|
14
|
+
*/
|
|
15
|
+
MRB_BEGIN_DECL
|
|
16
|
+
|
|
17
|
+
typedef struct mrb_shared_array {
|
|
18
|
+
int refcnt;
|
|
19
|
+
mrb_ssize len;
|
|
20
|
+
mrb_value *ptr;
|
|
21
|
+
} mrb_shared_array;
|
|
22
|
+
|
|
23
|
+
#if defined(MRB_32BIT) && defined(MRB_NO_BOXING) && !defined(MRB_USE_FLOAT32) && !defined(MRB_ARY_NO_EMBED)
|
|
24
|
+
# define MRB_ARY_NO_EMBED
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
#ifdef MRB_ARY_NO_EMBED
|
|
28
|
+
# define MRB_ARY_EMBED_LEN_MAX 0
|
|
29
|
+
#else
|
|
30
|
+
# define MRB_ARY_EMBED_LEN_MAX ((mrb_int)(sizeof(void*)*3/sizeof(mrb_value)))
|
|
31
|
+
mrb_static_assert(MRB_ARY_EMBED_LEN_MAX > 0, "MRB_ARY_EMBED_LEN_MAX > 0");
|
|
32
|
+
#endif
|
|
33
|
+
|
|
34
|
+
struct RArray {
|
|
35
|
+
MRB_OBJECT_HEADER;
|
|
36
|
+
union {
|
|
37
|
+
struct {
|
|
38
|
+
mrb_ssize len;
|
|
39
|
+
union {
|
|
40
|
+
mrb_ssize capa;
|
|
41
|
+
mrb_shared_array *shared;
|
|
42
|
+
} aux;
|
|
43
|
+
mrb_value *ptr;
|
|
44
|
+
} heap;
|
|
45
|
+
#ifndef MRB_ARY_NO_EMBED
|
|
46
|
+
mrb_value ary[MRB_ARY_EMBED_LEN_MAX];
|
|
47
|
+
#endif
|
|
48
|
+
} as;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
#define mrb_ary_ptr(v) ((struct RArray*)(mrb_ptr(v)))
|
|
52
|
+
#define mrb_ary_value(p) mrb_obj_value((void*)(p))
|
|
53
|
+
#define RARRAY(v) ((struct RArray*)(mrb_ptr(v)))
|
|
54
|
+
|
|
55
|
+
#ifdef MRB_ARY_NO_EMBED
|
|
56
|
+
#define ARY_EMBED_P(a) 0
|
|
57
|
+
#define ARY_UNSET_EMBED_FLAG(a) (void)0
|
|
58
|
+
#define ARY_EMBED_LEN(a) 0
|
|
59
|
+
#define ARY_SET_EMBED_LEN(a,len) (void)0
|
|
60
|
+
#define ARY_EMBED_PTR(a) 0
|
|
61
|
+
#else
|
|
62
|
+
#define MRB_ARY_EMBED_MASK 7
|
|
63
|
+
#define ARY_EMBED_P(a) ((a)->flags & MRB_ARY_EMBED_MASK)
|
|
64
|
+
#define ARY_UNSET_EMBED_FLAG(a) ((a)->flags &= ~(MRB_ARY_EMBED_MASK))
|
|
65
|
+
#define ARY_EMBED_LEN(a) ((mrb_int)(((a)->flags & MRB_ARY_EMBED_MASK) - 1))
|
|
66
|
+
#define ARY_SET_EMBED_LEN(a,len) ((a)->flags = ((a)->flags&~MRB_ARY_EMBED_MASK) | ((uint32_t)(len) + 1))
|
|
67
|
+
#define ARY_EMBED_PTR(a) ((a)->as.ary)
|
|
68
|
+
#endif
|
|
69
|
+
|
|
70
|
+
#define ARY_LEN(a) (ARY_EMBED_P(a)?ARY_EMBED_LEN(a):(mrb_int)(a)->as.heap.len)
|
|
71
|
+
#define ARY_PTR(a) (ARY_EMBED_P(a)?ARY_EMBED_PTR(a):(a)->as.heap.ptr)
|
|
72
|
+
#define RARRAY_LEN(a) ARY_LEN(RARRAY(a))
|
|
73
|
+
#define RARRAY_PTR(a) ARY_PTR(RARRAY(a))
|
|
74
|
+
#define ARY_SET_LEN(a,n) do {\
|
|
75
|
+
if (ARY_EMBED_P(a)) {\
|
|
76
|
+
mrb_assert((n) <= MRB_ARY_EMBED_LEN_MAX); \
|
|
77
|
+
ARY_SET_EMBED_LEN(a,n);\
|
|
78
|
+
}\
|
|
79
|
+
else\
|
|
80
|
+
(a)->as.heap.len = (n);\
|
|
81
|
+
} while (0)
|
|
82
|
+
#define ARY_CAPA(a) (ARY_EMBED_P(a)?MRB_ARY_EMBED_LEN_MAX:(a)->as.heap.aux.capa)
|
|
83
|
+
#define MRB_ARY_SHARED 256
|
|
84
|
+
#define ARY_SHARED_P(a) ((a)->flags & MRB_ARY_SHARED)
|
|
85
|
+
#define ARY_SET_SHARED_FLAG(a) ((a)->flags |= MRB_ARY_SHARED)
|
|
86
|
+
#define ARY_UNSET_SHARED_FLAG(a) ((a)->flags &= ~MRB_ARY_SHARED)
|
|
87
|
+
|
|
88
|
+
MRB_API void mrb_ary_modify(mrb_state*, struct RArray*);
|
|
89
|
+
MRB_API mrb_value mrb_ary_dup(mrb_state*, mrb_value ary);
|
|
90
|
+
MRB_API mrb_value mrb_ary_make_shared_copy(mrb_state*, mrb_value ary);
|
|
91
|
+
MRB_API mrb_value mrb_ary_new_capa(mrb_state*, mrb_int);
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* Initializes a new array.
|
|
95
|
+
*
|
|
96
|
+
* Equivalent to:
|
|
97
|
+
*
|
|
98
|
+
* Array.new
|
|
99
|
+
*
|
|
100
|
+
* @param mrb The mruby state reference.
|
|
101
|
+
* @return The initialized array.
|
|
102
|
+
*/
|
|
103
|
+
MRB_API mrb_value mrb_ary_new(mrb_state *mrb);
|
|
104
|
+
|
|
105
|
+
/*
|
|
106
|
+
* Initializes a new array with initial values
|
|
107
|
+
*
|
|
108
|
+
* Equivalent to:
|
|
109
|
+
*
|
|
110
|
+
* Array[value1, value2, ...]
|
|
111
|
+
*
|
|
112
|
+
* @param mrb The mruby state reference.
|
|
113
|
+
* @param size The number of values.
|
|
114
|
+
* @param vals The actual values.
|
|
115
|
+
* @return The initialized array.
|
|
116
|
+
*/
|
|
117
|
+
MRB_API mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
|
|
118
|
+
|
|
119
|
+
/*
|
|
120
|
+
* Initializes a new array with two initial values
|
|
121
|
+
*
|
|
122
|
+
* Equivalent to:
|
|
123
|
+
*
|
|
124
|
+
* Array[car, cdr]
|
|
125
|
+
*
|
|
126
|
+
* @param mrb The mruby state reference.
|
|
127
|
+
* @param car The first value.
|
|
128
|
+
* @param cdr The second value.
|
|
129
|
+
* @return The initialized array.
|
|
130
|
+
*/
|
|
131
|
+
MRB_API mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
|
|
132
|
+
|
|
133
|
+
/*
|
|
134
|
+
* Concatenate two arrays. The target array will be modified
|
|
135
|
+
*
|
|
136
|
+
* Equivalent to:
|
|
137
|
+
* ary.concat(other)
|
|
138
|
+
*
|
|
139
|
+
* @param mrb The mruby state reference.
|
|
140
|
+
* @param self The target array.
|
|
141
|
+
* @param other The array that will be concatenated to self.
|
|
142
|
+
*/
|
|
143
|
+
MRB_API void mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other);
|
|
144
|
+
|
|
145
|
+
/*
|
|
146
|
+
* Create an array from the input. It tries calling to_a on the
|
|
147
|
+
* value. If value does not respond to that, it creates a new
|
|
148
|
+
* array with just this value.
|
|
149
|
+
*
|
|
150
|
+
* @param mrb The mruby state reference.
|
|
151
|
+
* @param value The value to change into an array.
|
|
152
|
+
* @return An array representation of value.
|
|
153
|
+
*/
|
|
154
|
+
MRB_API mrb_value mrb_ary_splat(mrb_state *mrb, mrb_value value);
|
|
155
|
+
|
|
156
|
+
/*
|
|
157
|
+
* Pushes value into array.
|
|
158
|
+
*
|
|
159
|
+
* Equivalent to:
|
|
160
|
+
*
|
|
161
|
+
* ary << value
|
|
162
|
+
*
|
|
163
|
+
* @param mrb The mruby state reference.
|
|
164
|
+
* @param ary The array in which the value will be pushed
|
|
165
|
+
* @param value The value to be pushed into array
|
|
166
|
+
*/
|
|
167
|
+
MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value);
|
|
168
|
+
|
|
169
|
+
/*
|
|
170
|
+
* Pops the last element from the array.
|
|
171
|
+
*
|
|
172
|
+
* Equivalent to:
|
|
173
|
+
*
|
|
174
|
+
* ary.pop
|
|
175
|
+
*
|
|
176
|
+
* @param mrb The mruby state reference.
|
|
177
|
+
* @param ary The array from which the value will be popped.
|
|
178
|
+
* @return The popped value.
|
|
179
|
+
*/
|
|
180
|
+
MRB_API mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
|
|
181
|
+
|
|
182
|
+
/*
|
|
183
|
+
* Sets a value on an array at the given index
|
|
184
|
+
*
|
|
185
|
+
* Equivalent to:
|
|
186
|
+
*
|
|
187
|
+
* ary[n] = val
|
|
188
|
+
*
|
|
189
|
+
* @param mrb The mruby state reference.
|
|
190
|
+
* @param ary The target array.
|
|
191
|
+
* @param n The array index being referenced.
|
|
192
|
+
* @param val The value being set.
|
|
193
|
+
*/
|
|
194
|
+
MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
|
|
195
|
+
|
|
196
|
+
/*
|
|
197
|
+
* Replace the array with another array
|
|
198
|
+
*
|
|
199
|
+
* Equivalent to:
|
|
200
|
+
*
|
|
201
|
+
* ary.replace(other)
|
|
202
|
+
*
|
|
203
|
+
* @param mrb The mruby state reference
|
|
204
|
+
* @param self The target array.
|
|
205
|
+
* @param other The array to replace it with.
|
|
206
|
+
*/
|
|
207
|
+
MRB_API void mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other);
|
|
208
|
+
|
|
209
|
+
/*
|
|
210
|
+
* Unshift an element into the array
|
|
211
|
+
*
|
|
212
|
+
* Equivalent to:
|
|
213
|
+
*
|
|
214
|
+
* ary.unshift(item)
|
|
215
|
+
*
|
|
216
|
+
* @param mrb The mruby state reference.
|
|
217
|
+
* @param self The target array.
|
|
218
|
+
* @param item The item to unshift.
|
|
219
|
+
*/
|
|
220
|
+
MRB_API mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item);
|
|
221
|
+
|
|
222
|
+
/*
|
|
223
|
+
* Get nth element in the array
|
|
224
|
+
*
|
|
225
|
+
* Equivalent to:
|
|
226
|
+
*
|
|
227
|
+
* ary[offset]
|
|
228
|
+
*
|
|
229
|
+
* @param ary The target array.
|
|
230
|
+
* @param offset The element position (negative counts from the tail).
|
|
231
|
+
*/
|
|
232
|
+
MRB_API mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
|
|
233
|
+
#define mrb_ary_ref(mrb, ary, n) mrb_ary_entry(ary, n)
|
|
234
|
+
|
|
235
|
+
/*
|
|
236
|
+
* Replace subsequence of an array.
|
|
237
|
+
*
|
|
238
|
+
* Equivalent to:
|
|
239
|
+
*
|
|
240
|
+
* ary[head, len] = rpl
|
|
241
|
+
*
|
|
242
|
+
* @param mrb The mruby state reference.
|
|
243
|
+
* @param self The array from which the value will be partiality replaced.
|
|
244
|
+
* @param head Beginning position of a replacement subsequence.
|
|
245
|
+
* @param len Length of a replacement subsequence.
|
|
246
|
+
* @param rpl The array of replacement elements.
|
|
247
|
+
* It is possible to pass `mrb_undef_value()` instead of an empty array.
|
|
248
|
+
* @return The receiver array.
|
|
249
|
+
*/
|
|
250
|
+
MRB_API mrb_value mrb_ary_splice(mrb_state *mrb, mrb_value self, mrb_int head, mrb_int len, mrb_value rpl);
|
|
251
|
+
|
|
252
|
+
/*
|
|
253
|
+
* Shifts the first element from the array.
|
|
254
|
+
*
|
|
255
|
+
* Equivalent to:
|
|
256
|
+
*
|
|
257
|
+
* ary.shift
|
|
258
|
+
*
|
|
259
|
+
* @param mrb The mruby state reference.
|
|
260
|
+
* @param self The array from which the value will be shifted.
|
|
261
|
+
* @return The shifted value.
|
|
262
|
+
*/
|
|
263
|
+
MRB_API mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
|
|
264
|
+
|
|
265
|
+
/*
|
|
266
|
+
* Removes all elements from the array
|
|
267
|
+
*
|
|
268
|
+
* Equivalent to:
|
|
269
|
+
*
|
|
270
|
+
* ary.clear
|
|
271
|
+
*
|
|
272
|
+
* @param mrb The mruby state reference.
|
|
273
|
+
* @param self The target array.
|
|
274
|
+
* @return self
|
|
275
|
+
*/
|
|
276
|
+
MRB_API mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self);
|
|
277
|
+
|
|
278
|
+
/*
|
|
279
|
+
* Join the array elements together in a string
|
|
280
|
+
*
|
|
281
|
+
* Equivalent to:
|
|
282
|
+
*
|
|
283
|
+
* ary.join(sep="")
|
|
284
|
+
*
|
|
285
|
+
* @param mrb The mruby state reference.
|
|
286
|
+
* @param ary The target array
|
|
287
|
+
* @param sep The separator, can be NULL
|
|
288
|
+
*/
|
|
289
|
+
MRB_API mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep);
|
|
290
|
+
|
|
291
|
+
/*
|
|
292
|
+
* Update the capacity of the array
|
|
293
|
+
*
|
|
294
|
+
* @param mrb The mruby state reference.
|
|
295
|
+
* @param ary The target array.
|
|
296
|
+
* @param new_len The new capacity of the array
|
|
297
|
+
*/
|
|
298
|
+
MRB_API mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len);
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
MRB_END_DECL
|
|
302
|
+
|
|
303
|
+
#endif /* MRUBY_ARRAY_H */
|