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.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +195 -0
  3. data/exe/portable-mruby +6 -0
  4. data/lib/portable_mruby/binary_manager.rb +225 -0
  5. data/lib/portable_mruby/builder.rb +97 -0
  6. data/lib/portable_mruby/bytecode_compiler.rb +19 -0
  7. data/lib/portable_mruby/c_generator.rb +94 -0
  8. data/lib/portable_mruby/cli.rb +136 -0
  9. data/lib/portable_mruby/version.rb +6 -0
  10. data/lib/portable_mruby.rb +15 -0
  11. data/vendor/mruby/bin/mrbc.com +0 -0
  12. data/vendor/mruby/include/mrbconf.h +230 -0
  13. data/vendor/mruby/include/mruby/array.h +303 -0
  14. data/vendor/mruby/include/mruby/boxing_nan.h +169 -0
  15. data/vendor/mruby/include/mruby/boxing_no.h +59 -0
  16. data/vendor/mruby/include/mruby/boxing_word.h +251 -0
  17. data/vendor/mruby/include/mruby/class.h +104 -0
  18. data/vendor/mruby/include/mruby/common.h +118 -0
  19. data/vendor/mruby/include/mruby/compile.h +185 -0
  20. data/vendor/mruby/include/mruby/data.h +76 -0
  21. data/vendor/mruby/include/mruby/debug.h +75 -0
  22. data/vendor/mruby/include/mruby/dump.h +159 -0
  23. data/vendor/mruby/include/mruby/endian.h +44 -0
  24. data/vendor/mruby/include/mruby/error.h +132 -0
  25. data/vendor/mruby/include/mruby/gc.h +72 -0
  26. data/vendor/mruby/include/mruby/gems/mruby-dir/include/dir_hal.h +79 -0
  27. data/vendor/mruby/include/mruby/gems/mruby-io/include/io_hal.h +451 -0
  28. data/vendor/mruby/include/mruby/gems/mruby-io/include/mruby/ext/io.h +76 -0
  29. data/vendor/mruby/include/mruby/gems/mruby-socket/include/socket_hal.h +83 -0
  30. data/vendor/mruby/include/mruby/gems/mruby-time/include/mruby/time.h +27 -0
  31. data/vendor/mruby/include/mruby/hash.h +234 -0
  32. data/vendor/mruby/include/mruby/internal.h +274 -0
  33. data/vendor/mruby/include/mruby/irep.h +142 -0
  34. data/vendor/mruby/include/mruby/istruct.h +50 -0
  35. data/vendor/mruby/include/mruby/khash.h +455 -0
  36. data/vendor/mruby/include/mruby/mempool.h +19 -0
  37. data/vendor/mruby/include/mruby/numeric.h +174 -0
  38. data/vendor/mruby/include/mruby/object.h +45 -0
  39. data/vendor/mruby/include/mruby/opcode.h +69 -0
  40. data/vendor/mruby/include/mruby/ops.h +120 -0
  41. data/vendor/mruby/include/mruby/presym/disable.h +72 -0
  42. data/vendor/mruby/include/mruby/presym/enable.h +39 -0
  43. data/vendor/mruby/include/mruby/presym/id.h +1423 -0
  44. data/vendor/mruby/include/mruby/presym/scanning.h +81 -0
  45. data/vendor/mruby/include/mruby/presym/table.h +2847 -0
  46. data/vendor/mruby/include/mruby/presym.h +41 -0
  47. data/vendor/mruby/include/mruby/proc.h +186 -0
  48. data/vendor/mruby/include/mruby/range.h +77 -0
  49. data/vendor/mruby/include/mruby/re.h +16 -0
  50. data/vendor/mruby/include/mruby/string.h +428 -0
  51. data/vendor/mruby/include/mruby/throw.h +57 -0
  52. data/vendor/mruby/include/mruby/value.h +471 -0
  53. data/vendor/mruby/include/mruby/variable.h +108 -0
  54. data/vendor/mruby/include/mruby/version.h +143 -0
  55. data/vendor/mruby/include/mruby.h +1614 -0
  56. data/vendor/mruby/lib/libmruby.a +0 -0
  57. metadata +102 -0
@@ -0,0 +1,72 @@
1
+ /**
2
+ ** @file mruby/gc.h - garbage collector for mruby
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ */
6
+
7
+ #ifndef MRUBY_GC_H
8
+ #define MRUBY_GC_H
9
+
10
+ #include "common.h"
11
+
12
+ /**
13
+ * Uncommon memory management stuffs.
14
+ */
15
+ MRB_BEGIN_DECL
16
+
17
+
18
+ struct mrb_state;
19
+
20
+ #define MRB_EACH_OBJ_OK 0
21
+ #define MRB_EACH_OBJ_BREAK 1
22
+ typedef int (mrb_each_object_callback)(struct mrb_state *mrb, struct RBasic *obj, void *data);
23
+ void mrb_objspace_each_objects(struct mrb_state *mrb, mrb_each_object_callback *callback, void *data);
24
+ size_t mrb_objspace_page_slot_size(void);
25
+ MRB_API void mrb_free_context(struct mrb_state *mrb, struct mrb_context *c);
26
+
27
+ #ifndef MRB_GC_ARENA_SIZE
28
+ #define MRB_GC_ARENA_SIZE 100
29
+ #endif
30
+
31
+ typedef enum {
32
+ MRB_GC_STATE_ROOT = 0,
33
+ MRB_GC_STATE_MARK,
34
+ MRB_GC_STATE_SWEEP
35
+ } mrb_gc_state;
36
+
37
+ typedef struct mrb_gc {
38
+ struct mrb_heap_page *heaps; /* all heaps pages */
39
+ struct mrb_heap_page *free_heaps;/* heaps for allocation */
40
+ struct mrb_heap_page *sweeps; /* page where sweep starts */
41
+ struct RBasic *gray_list; /* list of gray objects to be traversed incrementally */
42
+ struct RBasic *atomic_gray_list; /* list of objects to be traversed atomically */
43
+ size_t live; /* count of live objects */
44
+ size_t live_after_mark; /* old generation objects */
45
+ size_t threshold; /* threshold to start GC */
46
+ size_t oldgen_threshold; /* threshold to kick major GC */
47
+ mrb_gc_state state; /* current state of gc */
48
+ int interval_ratio;
49
+ int step_ratio;
50
+ int current_white_part :2; /* make white object by white_part */
51
+ mrb_bool iterating :1; /* currently iterating over objects */
52
+ mrb_bool disabled :1; /* GC disabled */
53
+ mrb_bool generational :1; /* generational GC mode */
54
+ mrb_bool full :1; /* major GC mode */
55
+ mrb_bool out_of_memory :1; /* out-of-memory error occurred */
56
+
57
+ #ifdef MRB_GC_FIXED_ARENA
58
+ struct RBasic *arena[MRB_GC_ARENA_SIZE]; /* GC protection array */
59
+ #else
60
+ struct RBasic **arena; /* GC protection array */
61
+ int arena_capa; /* size of protection array */
62
+ #endif
63
+ int arena_idx;
64
+ } mrb_gc;
65
+
66
+ MRB_API mrb_bool mrb_object_dead_p(struct mrb_state *mrb, struct RBasic *object);
67
+
68
+ #define MRB_GC_RED 7
69
+
70
+ MRB_END_DECL
71
+
72
+ #endif /* MRUBY_GC_H */
@@ -0,0 +1,79 @@
1
+ /*
2
+ ** dir_hal.h - Directory HAL interface for mruby
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ **
6
+ ** Hardware Abstraction Layer for directory operations.
7
+ ** Provides platform-independent interface for filesystem directory operations.
8
+ */
9
+
10
+ #ifndef MRUBY_DIR_HAL_H
11
+ #define MRUBY_DIR_HAL_H
12
+
13
+ #include <mruby.h>
14
+
15
+ /*
16
+ * Platform-independent directory handle
17
+ * Each HAL implementation defines this structure internally
18
+ */
19
+ typedef struct mrb_dir_handle mrb_dir_handle;
20
+
21
+ /*
22
+ * Directory Operations
23
+ */
24
+
25
+ /* Open directory for reading. Returns handle or NULL on error (sets errno). */
26
+ mrb_dir_handle* mrb_hal_dir_open(mrb_state *mrb, const char *path);
27
+
28
+ /* Close directory handle. Returns 0 on success, -1 on error. */
29
+ int mrb_hal_dir_close(mrb_state *mrb, mrb_dir_handle *dir);
30
+
31
+ /* Read next entry from directory. Returns name or NULL at end/error. */
32
+ const char* mrb_hal_dir_read(mrb_state *mrb, mrb_dir_handle *dir);
33
+
34
+ /* Rewind directory to beginning */
35
+ void mrb_hal_dir_rewind(mrb_state *mrb, mrb_dir_handle *dir);
36
+
37
+ /*
38
+ * Optional Operations (may not be available on all platforms)
39
+ */
40
+
41
+ /* Seek to position in directory. Returns -1 if unsupported (sets errno to ENOSYS). */
42
+ int mrb_hal_dir_seek(mrb_state *mrb, mrb_dir_handle *dir, long pos);
43
+
44
+ /* Get current position in directory. Returns -1 if unsupported (sets errno to ENOSYS). */
45
+ long mrb_hal_dir_tell(mrb_state *mrb, mrb_dir_handle *dir);
46
+
47
+ /*
48
+ * Filesystem Operations
49
+ */
50
+
51
+ /* Create directory with mode (mode may be ignored on some platforms). Returns 0 on success, -1 on error. */
52
+ int mrb_hal_dir_mkdir(mrb_state *mrb, const char *path, int mode);
53
+
54
+ /* Remove empty directory. Returns 0 on success, -1 on error. */
55
+ int mrb_hal_dir_rmdir(mrb_state *mrb, const char *path);
56
+
57
+ /* Change current working directory. Returns 0 on success, -1 on error. */
58
+ int mrb_hal_dir_chdir(mrb_state *mrb, const char *path);
59
+
60
+ /* Get current working directory. Returns 0 on success, -1 on error. */
61
+ int mrb_hal_dir_getcwd(mrb_state *mrb, char *buf, size_t size);
62
+
63
+ /* Change root directory (privileged operation). Returns -1 if unsupported (sets errno to ENOSYS). */
64
+ int mrb_hal_dir_chroot(mrb_state *mrb, const char *path);
65
+
66
+ /* Check if path is a directory. Returns 1 if directory, 0 if not. */
67
+ int mrb_hal_dir_is_directory(mrb_state *mrb, const char *path);
68
+
69
+ /*
70
+ * HAL Initialization/Finalization
71
+ */
72
+
73
+ /* Initialize HAL (called once at gem initialization) */
74
+ void mrb_hal_dir_init(mrb_state *mrb);
75
+
76
+ /* Cleanup HAL (called once at gem finalization) */
77
+ void mrb_hal_dir_final(mrb_state *mrb);
78
+
79
+ #endif /* MRUBY_DIR_HAL_H */
@@ -0,0 +1,451 @@
1
+ /*
2
+ ** io_hal.h - IO Hardware Abstraction Layer (HAL)
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ **
6
+ ** This header defines the HAL interface for platform-specific I/O operations.
7
+ ** Platform-specific implementations (hal-posix-io, hal-win-io, etc.) must
8
+ ** provide all functions declared here.
9
+ */
10
+
11
+ #ifndef MRUBY_IO_HAL_H
12
+ #define MRUBY_IO_HAL_H
13
+
14
+ #include <mruby.h>
15
+ #include <stdint.h>
16
+
17
+ /*
18
+ * Platform-independent type definitions
19
+ */
20
+
21
+ /* File status structure - platform-independent representation */
22
+ typedef struct mrb_io_stat {
23
+ uint64_t st_dev; /* Device ID */
24
+ uint64_t st_ino; /* Inode number */
25
+ uint32_t st_mode; /* File mode/permissions */
26
+ uint32_t st_nlink; /* Number of hard links */
27
+ uint32_t st_uid; /* User ID */
28
+ uint32_t st_gid; /* Group ID */
29
+ uint64_t st_rdev; /* Device ID (if special file) */
30
+ int64_t st_size; /* File size in bytes */
31
+ int64_t st_atime; /* Last access time */
32
+ int64_t st_mtime; /* Last modification time */
33
+ int64_t st_ctime; /* Last status change time */
34
+ int64_t st_blksize; /* Block size for filesystem I/O */
35
+ int64_t st_blocks; /* Number of 512B blocks allocated */
36
+ } mrb_io_stat;
37
+
38
+ /* Timeval structure for select() */
39
+ typedef struct mrb_io_timeval {
40
+ int64_t tv_sec; /* Seconds */
41
+ int64_t tv_usec; /* Microseconds */
42
+ } mrb_io_timeval;
43
+
44
+ /* File descriptor set for select() */
45
+ typedef struct mrb_io_fdset mrb_io_fdset;
46
+
47
+ /*
48
+ * File mode constants (POSIX-style)
49
+ */
50
+
51
+ /* File type masks */
52
+ #define MRB_IO_S_IFMT 0170000 /* Type of file mask */
53
+ #define MRB_IO_S_IFSOCK 0140000 /* Socket */
54
+ #define MRB_IO_S_IFLNK 0120000 /* Symbolic link */
55
+ #define MRB_IO_S_IFREG 0100000 /* Regular file */
56
+ #define MRB_IO_S_IFBLK 0060000 /* Block device */
57
+ #define MRB_IO_S_IFDIR 0040000 /* Directory */
58
+ #define MRB_IO_S_IFCHR 0020000 /* Character device */
59
+ #define MRB_IO_S_IFIFO 0010000 /* FIFO */
60
+
61
+ /* File type test macros */
62
+ #define MRB_IO_S_ISREG(m) (((m) & MRB_IO_S_IFMT) == MRB_IO_S_IFREG)
63
+ #define MRB_IO_S_ISDIR(m) (((m) & MRB_IO_S_IFMT) == MRB_IO_S_IFDIR)
64
+ #define MRB_IO_S_ISCHR(m) (((m) & MRB_IO_S_IFMT) == MRB_IO_S_IFCHR)
65
+ #define MRB_IO_S_ISBLK(m) (((m) & MRB_IO_S_IFMT) == MRB_IO_S_IFBLK)
66
+ #define MRB_IO_S_ISFIFO(m) (((m) & MRB_IO_S_IFMT) == MRB_IO_S_IFIFO)
67
+ #define MRB_IO_S_ISLNK(m) (((m) & MRB_IO_S_IFMT) == MRB_IO_S_IFLNK)
68
+ #define MRB_IO_S_ISSOCK(m) (((m) & MRB_IO_S_IFMT) == MRB_IO_S_IFSOCK)
69
+
70
+ /* File lock constants */
71
+ #define MRB_IO_LOCK_SH 1 /* Shared lock */
72
+ #define MRB_IO_LOCK_EX 2 /* Exclusive lock */
73
+ #define MRB_IO_LOCK_NB 4 /* Non-blocking */
74
+ #define MRB_IO_LOCK_UN 8 /* Unlock */
75
+
76
+ /* Seek constants */
77
+ #define MRB_IO_SEEK_SET 0 /* Seek from beginning */
78
+ #define MRB_IO_SEEK_CUR 1 /* Seek from current position */
79
+ #define MRB_IO_SEEK_END 2 /* Seek from end */
80
+
81
+ /*
82
+ * HAL Interface - File Operations
83
+ */
84
+
85
+ /**
86
+ * Get file status by path
87
+ *
88
+ * @param mrb mruby state
89
+ * @param path File path (UTF-8)
90
+ * @param st Output stat structure
91
+ * @return 0 on success, -1 on error (sets errno)
92
+ */
93
+ int mrb_hal_io_stat(mrb_state *mrb, const char *path, mrb_io_stat *st);
94
+
95
+ /**
96
+ * Get file status by descriptor
97
+ *
98
+ * @param mrb mruby state
99
+ * @param fd File descriptor
100
+ * @param st Output stat structure
101
+ * @return 0 on success, -1 on error (sets errno)
102
+ */
103
+ int mrb_hal_io_fstat(mrb_state *mrb, int fd, mrb_io_stat *st);
104
+
105
+ /**
106
+ * Get link status (don't follow symlinks)
107
+ *
108
+ * @param mrb mruby state
109
+ * @param path File path (UTF-8)
110
+ * @param st Output stat structure
111
+ * @return 0 on success, -1 on error (sets errno)
112
+ */
113
+ int mrb_hal_io_lstat(mrb_state *mrb, const char *path, mrb_io_stat *st);
114
+
115
+ /**
116
+ * Change file permissions
117
+ *
118
+ * @param mrb mruby state
119
+ * @param path File path (UTF-8)
120
+ * @param mode New permission mode
121
+ * @return 0 on success, -1 on error (sets errno)
122
+ */
123
+ int mrb_hal_io_chmod(mrb_state *mrb, const char *path, uint32_t mode);
124
+
125
+ /**
126
+ * Set/get file creation mask
127
+ *
128
+ * @param mrb mruby state
129
+ * @param mask New umask value (if < 0, only returns current value)
130
+ * @return Previous umask value
131
+ */
132
+ uint32_t mrb_hal_io_umask(mrb_state *mrb, int32_t mask);
133
+
134
+ /**
135
+ * Truncate file to specified length
136
+ *
137
+ * @param mrb mruby state
138
+ * @param fd File descriptor
139
+ * @param length New file length
140
+ * @return 0 on success, -1 on error (sets errno)
141
+ */
142
+ int mrb_hal_io_ftruncate(mrb_state *mrb, int fd, int64_t length);
143
+
144
+ /**
145
+ * Apply or remove advisory lock on file
146
+ *
147
+ * @param mrb mruby state
148
+ * @param fd File descriptor
149
+ * @param operation Lock operation (MRB_IO_LOCK_*)
150
+ * @return 0 on success, -1 on error (sets errno)
151
+ */
152
+ int mrb_hal_io_flock(mrb_state *mrb, int fd, int operation);
153
+
154
+ /**
155
+ * Delete a file
156
+ *
157
+ * @param mrb mruby state
158
+ * @param path File path (UTF-8)
159
+ * @return 0 on success, -1 on error (sets errno)
160
+ */
161
+ int mrb_hal_io_unlink(mrb_state *mrb, const char *path);
162
+
163
+ /**
164
+ * Rename a file
165
+ *
166
+ * @param mrb mruby state
167
+ * @param oldpath Old file path (UTF-8)
168
+ * @param newpath New file path (UTF-8)
169
+ * @return 0 on success, -1 on error (sets errno)
170
+ */
171
+ int mrb_hal_io_rename(mrb_state *mrb, const char *oldpath, const char *newpath);
172
+
173
+ /**
174
+ * Create a symbolic link
175
+ *
176
+ * @param mrb mruby state
177
+ * @param target Target path (UTF-8)
178
+ * @param linkpath Link path (UTF-8)
179
+ * @return 0 on success, -1 on error (sets errno)
180
+ */
181
+ int mrb_hal_io_symlink(mrb_state *mrb, const char *target, const char *linkpath);
182
+
183
+ /**
184
+ * Read value of a symbolic link
185
+ *
186
+ * @param mrb mruby state
187
+ * @param path Symlink path (UTF-8)
188
+ * @param buf Buffer to store result (UTF-8)
189
+ * @param bufsize Buffer size
190
+ * @return Number of bytes placed in buf, -1 on error (sets errno)
191
+ */
192
+ int64_t mrb_hal_io_readlink(mrb_state *mrb, const char *path, char *buf, size_t bufsize);
193
+
194
+ /**
195
+ * Resolve pathname to absolute path
196
+ *
197
+ * @param mrb mruby state
198
+ * @param path Pathname (UTF-8)
199
+ * @param resolved Buffer for resolved path (must be at least PATH_MAX size)
200
+ * @return Pointer to resolved on success, NULL on error (sets errno)
201
+ */
202
+ char* mrb_hal_io_realpath(mrb_state *mrb, const char *path, char *resolved);
203
+
204
+ /**
205
+ * Get current working directory
206
+ *
207
+ * @param mrb mruby state
208
+ * @param buf Buffer to store result (UTF-8)
209
+ * @param size Buffer size
210
+ * @return Pointer to buf on success, NULL on error (sets errno)
211
+ */
212
+ char* mrb_hal_io_getcwd(mrb_state *mrb, char *buf, size_t size);
213
+
214
+ /**
215
+ * Get environment variable
216
+ *
217
+ * @param mrb mruby state
218
+ * @param name Variable name
219
+ * @return Value string (UTF-8) or NULL if not found
220
+ */
221
+ const char* mrb_hal_io_getenv(mrb_state *mrb, const char *name);
222
+
223
+ /**
224
+ * Get user's home directory
225
+ *
226
+ * @param mrb mruby state
227
+ * @param username User name (NULL for current user)
228
+ * @return Home directory path (UTF-8) or NULL on error (sets errno)
229
+ */
230
+ const char* mrb_hal_io_gethome(mrb_state *mrb, const char *username);
231
+
232
+ /*
233
+ * HAL Interface - Core I/O Operations
234
+ */
235
+
236
+ /**
237
+ * Open file
238
+ *
239
+ * @param mrb mruby state
240
+ * @param path File path (UTF-8)
241
+ * @param flags Open flags (O_RDONLY, O_WRONLY, O_RDWR, etc.)
242
+ * @param mode Creation mode (used if O_CREAT is set)
243
+ * @return File descriptor on success, -1 on error (sets errno)
244
+ */
245
+ int mrb_hal_io_open(mrb_state *mrb, const char *path, int flags, uint32_t mode);
246
+
247
+ /**
248
+ * Close file descriptor
249
+ *
250
+ * @param mrb mruby state
251
+ * @param fd File descriptor
252
+ * @return 0 on success, -1 on error (sets errno)
253
+ */
254
+ int mrb_hal_io_close(mrb_state *mrb, int fd);
255
+
256
+ /**
257
+ * Read from file descriptor
258
+ *
259
+ * @param mrb mruby state
260
+ * @param fd File descriptor
261
+ * @param buf Buffer to store data
262
+ * @param count Maximum bytes to read
263
+ * @return Number of bytes read, 0 on EOF, -1 on error (sets errno)
264
+ */
265
+ int64_t mrb_hal_io_read(mrb_state *mrb, int fd, void *buf, size_t count);
266
+
267
+ /**
268
+ * Write to file descriptor
269
+ *
270
+ * @param mrb mruby state
271
+ * @param fd File descriptor
272
+ * @param buf Data to write
273
+ * @param count Number of bytes to write
274
+ * @return Number of bytes written, -1 on error (sets errno)
275
+ */
276
+ int64_t mrb_hal_io_write(mrb_state *mrb, int fd, const void *buf, size_t count);
277
+
278
+ /**
279
+ * Reposition file offset
280
+ *
281
+ * @param mrb mruby state
282
+ * @param fd File descriptor
283
+ * @param offset Offset value
284
+ * @param whence Reference point (MRB_IO_SEEK_SET/CUR/END)
285
+ * @return New offset from beginning of file, -1 on error (sets errno)
286
+ */
287
+ int64_t mrb_hal_io_lseek(mrb_state *mrb, int fd, int64_t offset, int whence);
288
+
289
+ /**
290
+ * Duplicate file descriptor
291
+ *
292
+ * @param mrb mruby state
293
+ * @param fd File descriptor to duplicate
294
+ * @return New descriptor on success, -1 on error (sets errno)
295
+ */
296
+ int mrb_hal_io_dup(mrb_state *mrb, int fd);
297
+
298
+ /**
299
+ * Manipulate file descriptor
300
+ *
301
+ * @param mrb mruby state
302
+ * @param fd File descriptor
303
+ * @param cmd Command (F_GETFD, F_SETFD, etc.)
304
+ * @param arg Command argument
305
+ * @return Depends on command, -1 on error (sets errno)
306
+ */
307
+ int mrb_hal_io_fcntl(mrb_state *mrb, int fd, int cmd, int arg);
308
+
309
+ /**
310
+ * Check if descriptor refers to terminal
311
+ *
312
+ * @param mrb mruby state
313
+ * @param fd File descriptor
314
+ * @return 1 if TTY, 0 if not, -1 on error (sets errno)
315
+ */
316
+ int mrb_hal_io_isatty(mrb_state *mrb, int fd);
317
+
318
+ /**
319
+ * Create pipe
320
+ *
321
+ * @param mrb mruby state
322
+ * @param fds Array to store two file descriptors [read_end, write_end]
323
+ * @return 0 on success, -1 on error (sets errno)
324
+ */
325
+ int mrb_hal_io_pipe(mrb_state *mrb, int fds[2]);
326
+
327
+ /*
328
+ * HAL Interface - Process Operations
329
+ */
330
+
331
+ /**
332
+ * Spawn a new process
333
+ *
334
+ * Creates a new process and executes the command. File descriptors can be
335
+ * redirected for stdin/stdout/stderr (-1 means don't redirect).
336
+ *
337
+ * POSIX: Uses fork() + dup2() + execl()
338
+ * Windows: Uses CreateProcess() with STARTUPINFO
339
+ *
340
+ * @param mrb mruby state
341
+ * @param cmd Command to execute (shell command)
342
+ * @param stdin_fd File descriptor to use for stdin (-1 = don't redirect)
343
+ * @param stdout_fd File descriptor to use for stdout (-1 = don't redirect)
344
+ * @param stderr_fd File descriptor to use for stderr (-1 = don't redirect)
345
+ * @param pid Output parameter for process ID
346
+ * @return 0 on success, -1 on error (sets errno)
347
+ */
348
+ int mrb_hal_io_spawn_process(mrb_state *mrb, const char *cmd,
349
+ int stdin_fd, int stdout_fd, int stderr_fd,
350
+ int *pid);
351
+
352
+ /**
353
+ * Wait for process to change state
354
+ *
355
+ * @param mrb mruby state
356
+ * @param pid Process ID to wait for
357
+ * @param status Output parameter for exit status
358
+ * @param options Wait options (0 for blocking wait)
359
+ * @return Process ID on success, -1 on error (sets errno)
360
+ */
361
+ int mrb_hal_io_waitpid(mrb_state *mrb, int pid, int *status, int options);
362
+
363
+ /*
364
+ * HAL Interface - I/O Multiplexing
365
+ */
366
+
367
+ /**
368
+ * Allocate file descriptor set
369
+ *
370
+ * @param mrb mruby state
371
+ * @return Pointer to fdset or NULL on error
372
+ */
373
+ mrb_io_fdset* mrb_hal_io_fdset_alloc(mrb_state *mrb);
374
+
375
+ /**
376
+ * Free file descriptor set
377
+ *
378
+ * @param mrb mruby state
379
+ * @param fdset File descriptor set to free
380
+ */
381
+ void mrb_hal_io_fdset_free(mrb_state *mrb, mrb_io_fdset *fdset);
382
+
383
+ /**
384
+ * Clear file descriptor set
385
+ *
386
+ * @param mrb mruby state
387
+ * @param fdset File descriptor set
388
+ */
389
+ void mrb_hal_io_fdset_zero(mrb_state *mrb, mrb_io_fdset *fdset);
390
+
391
+ /**
392
+ * Add descriptor to set
393
+ *
394
+ * @param mrb mruby state
395
+ * @param fd File descriptor
396
+ * @param fdset File descriptor set
397
+ */
398
+ void mrb_hal_io_fdset_set(mrb_state *mrb, int fd, mrb_io_fdset *fdset);
399
+
400
+ /**
401
+ * Check if descriptor is in set
402
+ *
403
+ * @param mrb mruby state
404
+ * @param fd File descriptor
405
+ * @param fdset File descriptor set
406
+ * @return Non-zero if fd is in set, 0 otherwise
407
+ */
408
+ int mrb_hal_io_fdset_isset(mrb_state *mrb, int fd, mrb_io_fdset *fdset);
409
+
410
+ /**
411
+ * Monitor multiple file descriptors
412
+ *
413
+ * @param mrb mruby state
414
+ * @param nfds Highest file descriptor number + 1
415
+ * @param readfds Set of descriptors to check for reading (NULL = ignore)
416
+ * @param writefds Set of descriptors to check for writing (NULL = ignore)
417
+ * @param errorfds Set of descriptors to check for errors (NULL = ignore)
418
+ * @param timeout Timeout (NULL = block indefinitely)
419
+ * @return Number of ready descriptors, 0 on timeout, -1 on error (sets errno)
420
+ */
421
+ int mrb_hal_io_select(mrb_state *mrb, int nfds,
422
+ mrb_io_fdset *readfds,
423
+ mrb_io_fdset *writefds,
424
+ mrb_io_fdset *errorfds,
425
+ mrb_io_timeval *timeout);
426
+
427
+ /*
428
+ * HAL Initialization/Finalization
429
+ */
430
+
431
+ /**
432
+ * Initialize I/O HAL
433
+ *
434
+ * Called during gem initialization. Platform-specific HAL should perform
435
+ * any necessary setup here.
436
+ *
437
+ * @param mrb mruby state
438
+ */
439
+ void mrb_hal_io_init(mrb_state *mrb);
440
+
441
+ /**
442
+ * Finalize I/O HAL
443
+ *
444
+ * Called during gem finalization. Platform-specific HAL should perform
445
+ * any necessary cleanup here.
446
+ *
447
+ * @param mrb mruby state
448
+ */
449
+ void mrb_hal_io_final(mrb_state *mrb);
450
+
451
+ #endif /* MRUBY_IO_HAL_H */
@@ -0,0 +1,76 @@
1
+ /*
2
+ ** io.h - IO class
3
+ */
4
+
5
+ #ifndef MRUBY_IO_H
6
+ #define MRUBY_IO_H
7
+
8
+ #include <mruby.h>
9
+
10
+ #ifdef MRB_NO_STDIO
11
+ # error IO and File conflicts 'MRB_NO_STDIO' in your build configuration
12
+ #endif
13
+
14
+ #if defined(__cplusplus)
15
+ extern "C" {
16
+ #endif
17
+
18
+ #if defined(MRB_NO_IO_PREAD_PWRITE) || defined(MRB_WITHOUT_IO_PREAD_PWRITE)
19
+ # undef MRB_USE_IO_PREAD_PWRITE
20
+ #elif !defined(MRB_USE_IO_PREAD_PWRITE)
21
+ # if defined(__unix__) || defined(__MACH__) || defined(MRB_WITH_IO_PREAD_PWRITE)
22
+ # define MRB_USE_IO_PREAD_PWRITE
23
+ # endif
24
+ #endif
25
+
26
+ #define MRB_IO_BUF_SIZE 4096
27
+
28
+ struct mrb_io_buf {
29
+ short start;
30
+ short len;
31
+ char mem[MRB_IO_BUF_SIZE];
32
+ };
33
+
34
+ struct mrb_io {
35
+ unsigned int readable:1,
36
+ writable:1,
37
+ eof:1,
38
+ sync:1,
39
+ is_socket:1,
40
+ close_fd:1,
41
+ close_fd2:1;
42
+ int fd; /* file descriptor, or -1 */
43
+ int fd2; /* file descriptor to write if it's different from fd, or -1 */
44
+ int pid; /* child's pid (for pipes) */
45
+ struct mrb_io_buf *buf;
46
+ };
47
+
48
+ #define MRB_O_RDONLY 0x0000
49
+ #define MRB_O_WRONLY 0x0001
50
+ #define MRB_O_RDWR 0x0002
51
+ #define MRB_O_ACCMODE (MRB_O_RDONLY | MRB_O_WRONLY | MRB_O_RDWR)
52
+ #define MRB_O_NONBLOCK 0x0004
53
+ #define MRB_O_APPEND 0x0008
54
+ #define MRB_O_SYNC 0x0010
55
+ #define MRB_O_NOFOLLOW 0x0020
56
+ #define MRB_O_CREAT 0x0040
57
+ #define MRB_O_TRUNC 0x0080
58
+ #define MRB_O_EXCL 0x0100
59
+ #define MRB_O_NOCTTY 0x0200
60
+ #define MRB_O_DIRECT 0x0400
61
+ #define MRB_O_BINARY 0x0800
62
+ #define MRB_O_SHARE_DELETE 0x1000
63
+ #define MRB_O_TMPFILE 0x2000
64
+ #define MRB_O_NOATIME 0x4000
65
+ #define MRB_O_DSYNC 0x00008000
66
+ #define MRB_O_RSYNC 0x00010000
67
+
68
+ #define E_IO_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(IOError))
69
+ #define E_EOF_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(EOFError))
70
+
71
+ int mrb_io_fileno(mrb_state *mrb, mrb_value io);
72
+
73
+ #if defined(__cplusplus)
74
+ } /* extern "C" { */
75
+ #endif
76
+ #endif /* MRUBY_IO_H */