Skip to content

Commit ea73f25

Browse files
committed
Merge pull request #2468 from stevengj/pointerany
RFC: add unsafe_pointer_to_any (fixes #2458)
2 parents 97cf261 + 428405f commit ea73f25

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

base/exports.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ export
11261126
strerror,
11271127
unsafe_ref,
11281128
unsafe_assign,
1129+
unsafe_pointer_to_any,
11291130

11301131
# Macros
11311132
@str,

base/pointer.jl

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ unsafe_assign(p::Ptr{Any}, x::ANY, i::Integer) = pointerset(p, x, int(i))
3838
unsafe_assign{T}(p::Ptr{T}, x, i::Integer) = pointerset(p, convert(T, x), int(i))
3939
unsafe_assign{T}(p::Ptr{T}, x) = unsafe_assign(p, convert(T,x), 1)
4040

41+
# convert a boxed jl_value_t* to Any
42+
unsafe_pointer_to_any(p::Ptr) = pointerany(p)
43+
4144
integer(x::Ptr) = convert(Uint, x)
4245
unsigned(x::Ptr) = convert(Uint, x)
4346

src/intrinsics.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace JL_I {
3333
abs_float, copysign_float,
3434
flipsign_int,
3535
// pointer access
36-
pointerref, pointerset,
36+
pointerref, pointerset, pointerany,
3737
// checked arithmetic
3838
checked_sadd, checked_uadd, checked_ssub, checked_usub,
3939
checked_smul, checked_umul,
@@ -493,6 +493,14 @@ static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
493493
jl_error("pointerset: wrong number of arguments");
494494
return emit_pointerset(args[1], args[2], args[3], ctx);
495495
}
496+
if (f == pointerany) {
497+
if (nargs != 1)
498+
jl_error("pointerany: wrong number of arguments");
499+
if (!jl_is_cpointer_type(expr_type(args[1], ctx)))
500+
jl_error("pointerany: argument must be pointer type");
501+
return builder.CreateBitCast(emit_unboxed(args[1], ctx),
502+
jl_pvalue_llvmt);
503+
}
496504
if (nargs < 1) jl_error("invalid intrinsic call");
497505
Value *x = auto_unbox(args[1], ctx);
498506
Value *y = NULL;
@@ -1087,7 +1095,7 @@ extern "C" void jl_init_intrinsic_functions(void)
10871095
ADD_I(fptrunc32); ADD_I(fpext64);
10881096
ADD_I(abs_float); ADD_I(copysign_float);
10891097
ADD_I(flipsign_int);
1090-
ADD_I(pointerref); ADD_I(pointerset);
1098+
ADD_I(pointerref); ADD_I(pointerset); ADD_I(pointerany);
10911099
ADD_I(checked_sadd); ADD_I(checked_uadd);
10921100
ADD_I(checked_ssub); ADD_I(checked_usub);
10931101
ADD_I(checked_smul); ADD_I(checked_umul);

test/core.jl

+3
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ begin
473473
@test a2 == [101,102,103]
474474
end
475475

476+
@test unsafe_pointer_to_any(ccall(:jl_call1, Ptr{Void}, (Any,Any),
477+
x -> x+1, 314158)) == 314159
478+
476479
# issue #1287, combinations of try, catch, return
477480
begin
478481
local f, g

0 commit comments

Comments
 (0)