Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow JuliaWrapper for Julia functions; allow calling them #183

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions JuliaInterface/gap/calls.gi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ InstallMethod( CallFuncList,
return Julia.Core._apply( julia_obj, args );
end );

InstallMethod( CallFuncList,
[ "IsJuliaWrapper", "IsList" ],
function( julia_obj, args )
return CallFuncList( JuliaPointer( julia_obj ), args );
end );

InstallGlobalFunction( CallJuliaFunctionWithCatch,
function( julia_obj, args )
local res;
Expand Down
14 changes: 9 additions & 5 deletions JuliaInterface/src/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ jl_value_t * julia_gap(Obj obj)
#endif
) &&
CALL_1ARGS(JULIAINTERFACE_IsJuliaWrapper, obj) == True) {
Obj return_value = CALL_1ARGS(JULIAINTERFACE_JuliaPointer, obj);
if (!IS_JULIA_OBJ(return_value)) {
ErrorMayQuit("JuliaPointer must be a Julia object (not a %s)",
(Int)TNAM_OBJ(return_value), 0);
obj = CALL_1ARGS(JULIAINTERFACE_JuliaPointer, obj);
if (IS_JULIA_OBJ(obj)) {
return GET_JULIA_OBJ(obj);
}
return GET_JULIA_OBJ(return_value);
if (IS_JULIA_FUNC(obj)) {
return GET_JULIA_FUNC(obj);
}
// TODO: also allow wrapping auto-converted Julia objects???
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by this comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is conceivable that this one might want to uniformly wrap all and any object returned by some Julia function. But then this might also be a boolean or Int64. In that case, perhaps we'd want to do recursion here... but this is rather hypothetical, so better not to worry here too much for now. Hence the comment

ErrorMayQuit("JuliaPointer must be a Julia object (not a %s)",
(Int)TNAM_OBJ(obj), 0);
}
return (jl_value_t *)obj;
}
Expand Down
27 changes: 27 additions & 0 deletions JuliaInterface/tst/wrapper.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##
gap> START_TEST( "wrapper.tst" );

## create a type for wrapped objects
gap> fam := NewFamily("MyJuliaWrapperFamily");;
gap> type := NewType(fam, IsJuliaWrapper and IsAttributeStoringRep);;

## wrap a Julia object
gap> n := GAPToJulia(2^100);
<Julia: 1267650600228229401496703205376>
gap> N := Objectify(type, rec());;
gap> SetJuliaPointer(N, n);
gap> Julia.Base.typeof(N);
<Julia: BigInt>
gap> N(1);
Error, MethodError: objects of type BigInt are not callable

## wrap a Julia function
gap> f := Objectify(type, rec());;
gap> SetJuliaPointer(f, Julia.Base.sqrt);
gap> Julia.Base.typeof(f);
<Julia: typeof(sqrt)>
gap> f(4);
<Julia: 2.0>

##
gap> STOP_TEST( "wrapper.tst", 1 );