Skip to content
Newer
Older
100644 279 lines (236 sloc) 6.12 KB
9595725 update copyrights to 2011
Laurent Sansonetti authored Jan 15, 2011
1 /*
2 * This file is covered by the Ruby license. See COPYING for more details.
7d7d3e8 @ferrous26 Change ownership to The MacRuby Team and update copyrights
ferrous26 authored Apr 22, 2012
3 *
4 * Copyright (C) 2012, The MacRuby Team. All rights reserved.
9595725 update copyrights to 2011
Laurent Sansonetti authored Jan 15, 2011
5 * Copyright (C) 2007-2011, Apple Inc. All rights reserved.
6 * Copyright (C) 1993-2007 Yukihiro Matsumoto
7 */
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
8
d0898dd include/ruby/macruby.h -> macruby_internal.h
Laurent Sansonetti authored Jan 8, 2011
9 #include "macruby_internal.h"
7ed118f fix a bug when exceptions raised inside .rbo entry point functions wo…
Laurent Sansonetti authored Sep 24, 2010
10 #include "ruby/node.h"
11 #include "vm.h"
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
12 #include "dln.h"
7ed118f fix a bug when exceptions raised inside .rbo entry point functions wo…
Laurent Sansonetti authored Sep 24, 2010
13
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
14 #include <stdlib.h>
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
15 #include <alloca.h>
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
16 #include <string.h>
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
20 #include <sys/param.h>
21 #include <unistd.h>
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
22
99d8ed2 more work on the new objc dispatcher
Laurent Sansonetti authored Apr 18, 2009
23 #define FUNCNAME_PATTERN "Init_%s"
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
24
7a4fd1b start cleaning up the public header files
Laurent Sansonetti authored Jan 8, 2011
25 // In file.c
26 int eaccess(const char *path, int mode);
27
dbf338d @Watson1978 use strlcpy, memcpy and snprintf instead of strcpy, strncpy and sprintf
Watson1978 authored Jan 4, 2012
28 static size_t
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
29 init_funcname_len(char **buf, const char *file)
30 {
31 char *p;
32 const char *slash;
dbf338d @Watson1978 use strlcpy, memcpy and snprintf instead of strcpy, strncpy and sprintf
Watson1978 authored Jan 4, 2012
33 size_t len;
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
34
35 /* Load the file as an object one */
36 for (slash = file-1; *file; file++) /* Find position of last '/' */
37 if (*file == '/') slash = file;
38
39 len = strlen(FUNCNAME_PATTERN) + strlen(slash + 1);
40 *buf = xmalloc(len);
41 snprintf(*buf, len, FUNCNAME_PATTERN, slash + 1);
42 for (p = *buf; *p; p++) { /* Delete suffix if it exists */
406e3f8 Init_ functions may never have underscore characters
Laurent Sansonetti authored Sep 9, 2009
43 if (*p == '-') {
44 *p = '_';
45 }
46 }
47 for (p = *buf; *p; p++) { /* Delete suffix if it exists */
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
48 if (*p == '.') {
406e3f8 Init_ functions may never have underscore characters
Laurent Sansonetti authored Sep 9, 2009
49 *p = '\0';
50 break;
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
51 }
52 }
53 return p - *buf;
54 }
55
56 #define init_funcname(buf, file) do {\
dbf338d @Watson1978 use strlcpy, memcpy and snprintf instead of strcpy, strncpy and sprintf
Watson1978 authored Jan 4, 2012
57 size_t len = init_funcname_len(buf, file);\
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
58 char *tmp = ALLOCA_N(char, len+1);\
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
59 if (tmp == NULL) {\
60 xfree(*buf);\
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
61 rb_memerror();\
62 }\
dbf338d @Watson1978 use strlcpy, memcpy and snprintf instead of strcpy, strncpy and sprintf
Watson1978 authored Jan 4, 2012
63 strlcpy(tmp, *buf, len + 1);\
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
64 xfree(*buf);\
65 *buf = tmp;\
66 } while (0)
67
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
68 #include <dlfcn.h>
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
69 #include <mach-o/dyld.h>
70
71 static const char *
72 dln_strerror(void)
73 {
74 return (char*)dlerror();
75 }
76
9dfd3de moved the ruby_is_miniruby symbol somewhere else because main.o is no…
Laurent Sansonetti authored Aug 28, 2009
77 bool ruby_is_miniruby = false;
df691c9 make sure miniruby doesn't crash loading a C extension bundle
Laurent Sansonetti authored Aug 28, 2009
78
7ed118f fix a bug when exceptions raised inside .rbo entry point functions wo…
Laurent Sansonetti authored Sep 24, 2010
79 // This function is called back from .rbo files' gcc constructors, passing a
80 // pointer to their entry point function, during dlopen(). The entry point
81 // function is called right after dlopen() directly. This is because C++
82 // exceptions raised within a gcc constructor are not properly propagated.
83 static void *__mrep__ = NULL;
84 void
85 rb_mrep_register(void *imp)
86 {
87 __mrep__ = imp;
88 }
89
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
90 void*
ec94469 .rbo files are now initialized using a gcc constructor instead of an …
Laurent Sansonetti authored Sep 26, 2009
91 dln_load(const char *file, bool call_init)
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
92 {
df691c9 make sure miniruby doesn't crash loading a C extension bundle
Laurent Sansonetti authored Aug 28, 2009
93 if (ruby_is_miniruby) {
94 rb_raise(rb_eLoadError,
95 "miniruby can't load C extension bundles due to technical problems");
96 }
97
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
98 const char *error = 0;
99 #define DLN_ERROR() (error = dln_strerror(), strcpy(ALLOCA_N(char, strlen(error) + 1), error))
100
101 char *buf;
102 /* Load the file as an object one */
103 init_funcname(&buf, file);
104
105 {
106 void *handle;
107
108 /* Load file */
7ed118f fix a bug when exceptions raised inside .rbo entry point functions wo…
Laurent Sansonetti authored Sep 24, 2010
109 __mrep__ = NULL;
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
110 if ((handle = (void*)dlopen(file, RTLD_LAZY|RTLD_GLOBAL)) == NULL) {
111 error = dln_strerror();
112 goto failed;
113 }
114
ec94469 .rbo files are now initialized using a gcc constructor instead of an …
Laurent Sansonetti authored Sep 26, 2009
115 if (call_init) {
116 void (*init_fct)();
117 init_fct = (void(*)())dlsym(handle, buf);
118 if (init_fct == NULL) {
119 error = DLN_ERROR();
120 dlclose(handle);
121 goto failed;
122 }
123 /* Call the init code */
68ac3fc @takaokouji fixed RoxorVM::pop_outer() leaks memory. (fixes #1267)
takaokouji authored May 18, 2011
124 rb_vm_dln_load(init_fct, NULL);
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
125 }
7ed118f fix a bug when exceptions raised inside .rbo entry point functions wo…
Laurent Sansonetti authored Sep 24, 2010
126 else {
864df99 fix a problem in #load: instead of crashing if we cannot locate the e…
Laurent Sansonetti authored Jan 28, 2011
127 if (__mrep__ == NULL) {
128 rb_raise(rb_eLoadError, "Can't load %s: entry point function not located (this can happen when you load twice the same .rbo file with a different case on a case-insensitive filesystem)", file);
129 }
68ac3fc @takaokouji fixed RoxorVM::pop_outer() leaks memory. (fixes #1267)
takaokouji authored May 18, 2011
130 rb_vm_dln_load(NULL, (IMP)__mrep__);
7ed118f fix a bug when exceptions raised inside .rbo entry point functions wo…
Laurent Sansonetti authored Sep 24, 2010
131 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
132
133 return handle;
134 }
135
136 failed:
137 rb_loaderror("%s - %s", error, file);
138
139 return 0; /* dummy return */
140 }
141
142 static char *dln_find_1(const char *fname, const char *path, char *buf, int size, int exe_flag);
143
144 char *
145 dln_find_exe_r(const char *fname, const char *path, char *buf, int size)
146 {
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
147 if (path == NULL) {
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
148 path = getenv(PATH_ENV);
149 }
150
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
151 if (path == NULL) {
152 path = "/usr/local/bin:/usr/bin:/bin:.";
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
153 }
154 return dln_find_1(fname, path, buf, size, 1);
155 }
156
157 char *
158 dln_find_file_r(const char *fname, const char *path, char *buf, int size)
159 {
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
160 if (path == NULL) {
161 path = ".";
162 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
163 return dln_find_1(fname, path, buf, size, 0);
164 }
165
166 static char fbuf[MAXPATHLEN];
167
168 char *
169 dln_find_exe(const char *fname, const char *path)
170 {
171 return dln_find_exe_r(fname, path, fbuf, sizeof(fbuf));
172 }
173
174 char *
175 dln_find_file(const char *fname, const char *path)
176 {
177 return dln_find_file_r(fname, path, fbuf, sizeof(fbuf));
178 }
179
180 static char *
181 dln_find_1(const char *fname, const char *path, char *fbuf, int size,
182 int exe_flag /* non 0 if looking for executable. */)
183 {
184 register const char *dp;
185 register const char *ep;
186 register char *bp;
187 struct stat st;
188
189 #define RETURN_IF(expr) if (expr) return (char *)fname;
190
191 RETURN_IF(!fname);
192 RETURN_IF(fname[0] == '/');
193 RETURN_IF(strncmp("./", fname, 2) == 0 || strncmp("../", fname, 3) == 0);
194 RETURN_IF(exe_flag && strchr(fname, '/'));
195
196 #undef RETURN_IF
197
198 for (dp = path;; dp = ++ep) {
199 register int l;
200 int i;
201 int fspace;
202
203 /* extract a component */
204 ep = strchr(dp, PATH_SEP[0]);
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
205 if (ep == NULL) {
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
206 ep = dp+strlen(dp);
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
207 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
208
209 /* find the length of that component */
210 l = ep - dp;
211 bp = fbuf;
212 fspace = size - 2;
213 if (l > 0) {
214 /*
215 ** If the length of the component is zero length,
216 ** start from the current directory. If the
217 ** component begins with "~", start from the
218 ** user's $HOME environment variable. Otherwise
219 ** take the path literally.
220 */
221
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
222 if (*dp == '~' && (l == 1 || dp[1] == '/')) {
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
223 char *home;
224
225 home = getenv("HOME");
226 if (home != NULL) {
227 i = strlen(home);
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
228 if ((fspace -= i) < 0) {
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
229 goto toolong;
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
230 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
231 memcpy(bp, home, i);
232 bp += i;
233 }
234 dp++;
235 l--;
236 }
237 if (l > 0) {
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
238 if ((fspace -= l) < 0) {
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
239 goto toolong;
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
240 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
241 memcpy(bp, dp, l);
242 bp += l;
243 }
244
245 /* add a "/" between directory and filename */
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
246 if (ep[-1] != '/') {
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
247 *bp++ = '/';
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
248 }
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
249 }
250
251 /* now append the file name */
252 i = strlen(fname);
253 if ((fspace -= i) < 0) {
254 toolong:
255 fprintf(stderr, "openpath: pathname too long (ignored)\n");
256 *bp = '\0';
257 fprintf(stderr, "\tDirectory \"%s\"\n", fbuf);
258 fprintf(stderr, "\tFile \"%s\"\n", fname);
259 goto next;
260 }
261 memcpy(bp, fname, i + 1);
262
263 if (stat(fbuf, &st) == 0) {
4a8c533 don't attempt to #load directories
Laurent Sansonetti authored Aug 17, 2010
264 if (!S_ISDIR(st.st_mode)
265 && (exe_flag == 0 || eaccess(fbuf, X_OK) == 0)) {
f11ed08 fixed the GC-related LoadError problem when starting the rubyspecs, r…
Laurent Sansonetti authored Mar 24, 2009
266 return fbuf;
9c1d230 committing experimental branch content
Laurent Sansonetti authored Mar 11, 2009
267 }
268 }
269
270 next:
271 /* if not, and no other alternatives, life is bleak */
272 if (*ep == '\0') {
273 return NULL;
274 }
275
276 /* otherwise try the next component in the search path */
277 }
278 }
Something went wrong with that request. Please try again.