FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/cext/src/object.cpp at prepend · MSNexploder/jruby · GitHub
MSNexploder
/
jruby
Public
forked from
jruby/jruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
jruby
/
cext
/
src
/
object.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
372 lines (310 loc) · 8.64 KB
Breadcrumbs
jruby
/
cext
/
src
/
object.cpp
Copy path
File metadata and controls
372 lines (310 loc) · 8.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright (C) 1993-2003 Yukihiro Matsumoto
* Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
* Copyright (C) 2010 Tim Felgentreff
*
* This file is part of jruby-cext.
*
* This code is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details.
*
* You should have received a copy of the GNU General Public License
* version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
*/
#
include
<
stdio.h
>
#
include
<
stdlib.h
>
#
ifdef
__sun
#
include
<
alloca.h
>
#
endif
#
include
"
jruby.h
"
#
include
"
ruby.h
"
#
include
"
JLocalEnv.h
"
#
include
"
Handle.h
"
using
namespace
jruby
;
extern
"
C
"
bool
jruby_obj_frozen
(
VALUE
obj)
{
return
callMethod
(obj,
"
frozen?
"
,
0
) == Qtrue;
}
static
VALUE
convert_type
(
VALUE
val,
const
char
* type_name,
const
char
* method,
int
raise)
{
ID
m =
rb_intern
(method);
if
(!
rb_respond_to
(val, m)) {
if
(raise) {
rb_raise
(rb_eTypeError,
"
can't convert %s into %s
"
,
NIL_P
(val) ?
"
nil
"
:
val == Qtrue ?
"
true
"
:
val == Qfalse ?
"
false
"
:
rb_obj_classname
(val),
type_name);
}
else
{
return
Qnil;
}
}
return
rb_funcall
(val, m,
0
);
}
extern
"
C
"
VALUE
rb_obj_freeze
(
VALUE
obj)
{
return
callMethodA
(obj,
"
freeze
"
,
0
,
NULL
);
}
extern
"
C
"
char
*
rb_obj_classname
(
VALUE
obj)
{
return
rb_class2name
(
rb_class_of
(obj));
}
extern
"
C
"
void
rb_extend_object
(
VALUE
object,
VALUE
module
)
{
callMethod
(object,
"
extend
"
,
1
,
module
);
}
extern
"
C
"
int
rb_respond_to
(
VALUE
obj,
ID
id)
{
JLocalEnv env;
jboolean ret = env->
CallBooleanMethod
(
valueToObject
(env, obj), IRubyObject_respondsTo_method,
idToString
(env, id));
checkExceptions
(env);
return
ret !=
JNI_FALSE
;
}
extern
"
C
"
int
rb_obj_respond_to
(
VALUE
obj,
ID
id,
int
include_private)
{
return
RTEST
(
callMethod
(obj,
"
respond_to?
"
,
2
,
ID2SYM
(id), include_private ? Qtrue : Qfalse));
}
extern
"
C
"
VALUE
rb_convert_type
(
VALUE
val,
int
type,
const
char
* type_name,
const
char
* method)
{
VALUE
v;
if
(
TYPE
(val) == type) {
return
val;
}
v =
convert_type
(val, type_name, method, Qtrue);
if
(
TYPE
(v) != type) {
rb_raise
(rb_eTypeError,
"
%s#%s should return %s
"
,
rb_obj_classname
(val), method, type_name);
}
return
v;
}
extern
"
C
"
VALUE
rb_check_convert_type
(
VALUE
val,
int
type,
const
char
* type_name,
const
char
* method)
{
if
(
TYPE
(val) == type) {
return
val;
}
return
convert_type
(val, type_name, method,
0
);
}
extern
"
C
"
VALUE
rb_check_to_integer
(
VALUE
object_handle,
const
char
*method_name)
{
if
(
FIXNUM_P
(object_handle)) {
return
object_handle;
}
VALUE
result =
rb_check_convert_type
(object_handle,
0
,
"
Integer
"
, method_name);
if
(
rb_obj_is_kind_of
(result, rb_cInteger)) {
return
result;
}
return
Qnil;
}
extern
"
C
"
VALUE
rb_iv_get
(
VALUE
obj,
const
char
* name)
{
JLocalEnv env;
char
var_name[
strlen
(name) +
2
];
(name[
0
] !=
'
@
'
) ?
strcpy
(var_name,
"
@
"
)[
0
] : var_name[
0
] =
'
\0
'
;
strcat
(var_name, name);
jobject retval = env->
CallObjectMethod
(
valueToObject
(env, obj), RubyBasicObject_getInstanceVariable_method,
env->
NewStringUTF
(var_name));
checkExceptions
(env);
return
objectToValue
(env, retval);
}
extern
"
C
"
VALUE
rb_iv_set
(
VALUE
obj,
const
char
* name,
VALUE
value)
{
JLocalEnv env;
char
var_name[
strlen
(name) +
2
];
(name[
0
] !=
'
@
'
) ?
strcpy
(var_name,
"
@
"
)[
0
] : var_name[
0
] =
'
\0
'
;
strcat
(var_name, name);
jobject retval = env->
CallObjectMethod
(
valueToObject
(env, obj), RubyBasicObject_setInstanceVariable_method,
env->
NewStringUTF
(var_name),
valueToObject
(env, value));
checkExceptions
(env);
return
objectToValue
(env, retval);
}
extern
"
C
"
VALUE
rb_ivar_get
(
VALUE
obj,
ID
ivar_name)
{
return
rb_iv_get
(obj,
rb_id2name
(ivar_name));
}
extern
"
C
"
VALUE
rb_ivar_set
(
VALUE
obj,
ID
ivar_name,
VALUE
value)
{
return
rb_iv_set
(obj,
rb_id2name
(ivar_name), value);
}
extern
"
C
"
VALUE
rb_ivar_defined
(
VALUE
obj,
ID
ivar)
{
JLocalEnv env;
const
char
* name =
rb_id2name
(ivar);
char
var_name[
strlen
(name) +
2
];
(name[
0
] !=
'
@
'
) ?
strcpy
(var_name,
"
@
"
)[
0
] : var_name[
0
] =
'
\0
'
;
strcat
(var_name, name);
jboolean retval = env->
CallBooleanMethod
(
valueToObject
(env, obj), RubyBasicObject_hasInstanceVariable_method,
env->
NewStringUTF
(var_name));
checkExceptions
(env);
return
(retval ==
JNI_TRUE
) ? Qtrue : Qfalse;
}
extern
"
C
"
void
rb_obj_call_init
(
VALUE
recv,
int
arg_count,
VALUE
* args)
{
callMethodANonConst
(recv,
"
initialize
"
, arg_count, args);
}
extern
"
C
"
VALUE
rb_obj_is_kind_of
(
VALUE
obj,
VALUE
module
)
{
return
callMethod
(obj,
"
kind_of?
"
,
1
,
module
);
}
extern
"
C
"
VALUE
rb_obj_is_instance_of
(
VALUE
obj,
VALUE
klass)
{
return
callMethodA
(obj,
"
instance_of?
"
,
1
, &klass);
}
extern
"
C
"
VALUE
rb_obj_taint
(
VALUE
obj)
{
return
callMethodA
(obj,
"
taint
"
,
0
,
NULL
);
}
extern
"
C
"
VALUE
rb_obj_tainted
(
VALUE
obj)
{
return
callMethodA
(obj,
"
tainted?
"
,
0
,
NULL
);
}
extern
"
C
"
VALUE
rb_attr_get
(
VALUE
obj,
ID
id)
{
return
callMethod
(obj,
"
instance_variable_get
"
,
1
,
ID2SYM
(id));
}
extern
"
C
"
VALUE
rb_obj_as_string
(
VALUE
obj)
{
VALUE
str;
if
(
TYPE
(obj) ==
T_STRING
) {
return
obj;
}
str =
callMethodA
(obj,
"
to_s
"
,
0
,
NULL
);
if
(
TYPE
(str) !=
T_STRING
) {
return
rb_any_to_s
(obj);
}
if
(
OBJ_TAINTED
(obj)) {
OBJ_TAINT
(str);
}
return
str;
}
extern
"
C
"
VALUE
rb_to_integer
(
VALUE
val,
const
char
*method)
{
VALUE
v;
if
(
FIXNUM_P
(val))
return
val;
if
(
TYPE
(val) ==
T_BIGNUM
)
return
val;
v =
convert_type
(val,
"
Integer
"
, method,
1
);
if
(!
rb_obj_is_kind_of
(v, rb_cInteger)) {
const
char
*cname =
rb_obj_classname
(val);
rb_raise
(rb_eTypeError,
"
can't convert %s to Integer (%s#%s gives %s)
"
,
cname, cname, method,
rb_obj_classname
(v));
}
return
v;
}
extern
"
C
"
VALUE
rb_inspect
(
VALUE
obj)
{
return
rb_obj_as_string
(
callMethodA
(obj,
"
inspect
"
,
0
,
0
));
}
extern
"
C
"
VALUE
rb_any_to_s
(
VALUE
obj)
{
char
* buf;
int
len =
128
, buflen;
do
{
buf = (
char
*)
alloca
(buflen = len);
len =
snprintf
(buf, buflen,
"
#<%s:%p>
"
,
rb_obj_classname
(obj), (
void
*) obj);
}
while
(len >= buflen);
return
rb_str_new_cstr
(buf);
}
extern
"
C
"
void
rb_check_frozen
(
VALUE
obj)
{
if
(
OBJ_FROZEN
(obj)) {
rb_raise
(
is1_9
() ? rb_eRuntimeError : rb_eTypeError,
"
can't modify frozen %s
"
,
rb_obj_classname
(obj));
}
}
extern
"
C
"
VALUE
rb_singleton_class
(
VALUE
obj)
{
JLocalEnv env;
jmethodID IRubyObject_getSingletonClass_method =
getCachedMethodID
(env, IRubyObject_class,
"
getSingletonClass
"
,
"
()Lorg/jruby/RubyClass;
"
);
jobject singleton = env->
CallObjectMethod
(
valueToObject
(env, obj), IRubyObject_getSingletonClass_method);
checkExceptions
(env);
return
objectToValue
(env, singleton);
}
extern
"
C
"
VALUE
rb_class_inherited_p
(
VALUE
mod,
VALUE
arg)
{
if
(
TYPE
(arg) !=
T_MODULE
&&
TYPE
(arg) !=
T_CLASS
) {
rb_raise
(rb_eTypeError,
"
compared with non class/module
"
);
}
return
callMethodA
(mod,
"
<=
"
,
1
, &arg);
}
extern
"
C
"
VALUE
rb_class_superclass
(
VALUE
klass)
{
return
callMethodA
(klass,
"
superclass
"
,
0
,
NULL
);
}
extern
"
C
"
VALUE
rb_obj_dup
(
VALUE
obj)
{
return
callMethod
(obj,
"
dup
"
,
0
);
}
extern
"
C
"
VALUE
rb_obj_clone
(
VALUE
obj)
{
return
callMethod
(obj,
"
clone
"
,
0
);
}
extern
"
C
"
VALUE
rb_obj_id
(
VALUE
obj)
{
return
callMethod
(obj,
"
object_id
"
,
0
);
}
extern
"
C
"
VALUE
rb_equal
(
VALUE
obj,
VALUE
other)
{
return
RTEST
(
callMethod
(obj,
"
==
"
,
1
, other)) ? Qtrue : Qfalse;
}
extern
"
C
"
int
rb_eql
(
VALUE
obj1,
VALUE
obj2)
{
return
RTEST
(
callMethodA
(obj1,
"
eql?
"
,
1
, &obj2));
}
extern
"
C
"
void
jruby_infect
(
VALUE
object1,
VALUE
object2)
{
if
(
OBJ_TAINTED
(object1)) {
JLocalEnv env;
jmethodID mid =
getCachedMethodID
(env, IRubyObject_class,
"
infectBy
"
,
"
(Lorg/jruby/runtime/builtin/IRubyObject;)Lorg/jruby/runtime/builtin/IRubyObject;
"
);
env->
CallObjectMethod
(
valueToObject
(env, object2), mid, object1);
checkExceptions
(env);
}
}
extern
"
C
"
VALUE
rb_hash
(
VALUE
obj)
{
VALUE
hash =
callMethod
(obj,
"
hash
"
,
0
);
return
convert_type
(hash,
"
Fixnum
"
,
"
to_int
"
,
true
);
}
Back
|
FazBrowse Home
|
New Git URL