diff --git a/JuliaInterface/src/JuliaInterface.c b/JuliaInterface/src/JuliaInterface.c index bc233b11a..e296c9f3a 100644 --- a/JuliaInterface/src/JuliaInterface.c +++ b/JuliaInterface/src/JuliaInterface.c @@ -870,6 +870,10 @@ static Obj FuncJuliaSetVal(Obj self, Obj name, Obj julia_val) // currently bound to the julia identifier . static Obj Func_JuliaGetGlobalVariable(Obj self, Obj name) { + if (!IS_STRING_REP(name)) { + ErrorMayQuit("_JuliaGetGlobalVariable: must be a string", 0, 0); + } + jl_sym_t * symbol = jl_symbol(CSTR_STRING(name)); if (!jl_boundp(jl_main_module, symbol)) { return Fail; @@ -882,21 +886,27 @@ static Obj Func_JuliaGetGlobalVariable(Obj self, Obj name) // currently bound to the julia identifier .. static Obj Func_JuliaGetGlobalVariableByModule(Obj self, Obj name, Obj module) { - jl_module_t * module_t = 0; + if (!IS_STRING_REP(name)) { + ErrorMayQuit("_JuliaGetGlobalVariableByModule: must be a string", 0, 0); + } + + jl_module_t * m = 0; if (IS_JULIA_OBJ(module)) { - module_t = (jl_module_t *)GET_JULIA_OBJ(module); + m = (jl_module_t *)GET_JULIA_OBJ(module); + if (!jl_is_module(m)) + m = 0; } else if (IS_STRING_REP(module)) { - module_t = get_module_from_string(CSTR_STRING(module)); + m = get_module_from_string(CSTR_STRING(module)); } - if (!module_t) { - ErrorMayQuit("second argument is not a module", 0, 0); + if (!m) { + ErrorMayQuit("_JuliaGetGlobalVariableByModule: must be a string or a Julia module", 0, 0); } jl_sym_t * symbol = jl_symbol(CSTR_STRING(name)); - if (!jl_boundp(module_t, symbol)) { + if (!jl_boundp(m, symbol)) { return Fail; } - jl_value_t * value = jl_get_global(module_t, symbol); + jl_value_t * value = jl_get_global(m, symbol); return NewJuliaObj(value); } @@ -905,10 +915,18 @@ static Obj Func_JuliaGetGlobalVariableByModule(Obj self, Obj name, Obj module) // must be a julia object GAP object, and a string. static Obj FuncJuliaGetFieldOfObject(Obj self, Obj super_obj, Obj field_name) { + if (!IS_JULIA_OBJ(super_obj)) { + ErrorMayQuit("JuliaGetFieldOfObject: must be a Julia object", 0, 0); + } + if (!IS_STRING_REP(field_name)) { + ErrorMayQuit("JuliaGetFieldOfObject: must be a string", 0, 0); + } + + jl_value_t * extracted_superobj = GET_JULIA_OBJ(super_obj); + // It suffices to use JULIAINTERFACE_EXCEPTION_HANDLER here, as // jl_get_field is part of the jlapi, so don't have to be wrapped in // JL_TRY/JL_CATCH. - jl_value_t * extracted_superobj = GET_JULIA_OBJ(super_obj); jl_value_t * field_value = jl_get_field(extracted_superobj, CSTR_STRING(field_name)); JULIAINTERFACE_EXCEPTION_HANDLER diff --git a/JuliaInterface/tst/calls.tst b/JuliaInterface/tst/calls.tst index f61a3c5ac..257029cd3 100644 --- a/JuliaInterface/tst/calls.tst +++ b/JuliaInterface/tst/calls.tst @@ -13,32 +13,32 @@ gap> f(); # -gap> f(1); - +gap> f(true); + # -gap> f(1,2); - +gap> f(true,2); + # -gap> f(1,2,3); - +gap> f(true,2,3); + # -gap> f(1,2,3,4); - +gap> f(true,2,3,4); + # -gap> f(1,2,3,4,5); - +gap> f(true,2,3,4,5); + # -gap> f(1,2,3,4,5,6); - +gap> f(true,2,3,4,5,6); + # -gap> f(1,2,3,4,5,6,7); - +gap> f(true,2,3,4,5,6,7); + # non-variadic functions gap> f0 := JuliaEvalString("function f0() end");; @@ -55,32 +55,32 @@ gap> f0(); # -gap> f1(1); - +gap> f1(true); + # -gap> f2(1,2); - +gap> f2(true,2); + # -gap> f3(1,2,3); - +gap> f3(true,2,3); + # -gap> f4(1,2,3,4); - +gap> f4(true,2,3,4); + # -gap> f5(1,2,3,4,5); - +gap> f5(true,2,3,4,5); + # -gap> f6(1,2,3,4,5,6); - +gap> f6(true,2,3,4,5,6); + # -gap> f7(1,2,3,4,5,6,7); - +gap> f7(true,2,3,4,5,6,7); + # # calls via function wrappers @@ -94,32 +94,32 @@ gap> fw(); # -gap> fw(1); - +gap> fw(true); + # -gap> fw(1,2); - +gap> fw(true,2); + # -gap> fw(1,2,3); - +gap> fw(true,2,3); + # -gap> fw(1,2,3,4); - +gap> fw(true,2,3,4); + # -gap> fw(1,2,3,4,5); - +gap> fw(true,2,3,4,5); + # -gap> fw(1,2,3,4,5,6); - +gap> fw(true,2,3,4,5,6); + # -gap> fw(1,2,3,4,5,6,7); - +gap> fw(true,2,3,4,5,6,7); + # non-variadic functions gap> f0w := JuliaFunction("f0");; @@ -136,32 +136,113 @@ gap> f0w(); # -gap> f1w(1); - +gap> f1w(true); + # -gap> f2w(1,2); - +gap> f2w(true,2); + # -gap> f3w(1,2,3); - +gap> f3w(true,2,3); + # -gap> f4w(1,2,3,4); - +gap> f4w(true,2,3,4); + # -gap> f5w(1,2,3,4,5); - +gap> f5w(true,2,3,4,5); + # -gap> f6w(1,2,3,4,5,6); - +gap> f6w(true,2,3,4,5,6); + # -gap> f7w(1,2,3,4,5,6,7); - +gap> f7w(true,2,3,4,5,6,7); + + +# +# calls via function wrappers, auto conversion turned off +# + +# variadic function +gap> fwN := _JuliaFunction("f", false);; + +# +gap> fwN(); + + +# +gap> fwN(true); + + +# +gap> fwN(true,2); + + +# +gap> fwN(true,2,3); + + +# +gap> fwN(true,2,3,4); + + +# +gap> fwN(true,2,3,4,5); + + +# +gap> fwN(true,2,3,4,5,6); + + +# +gap> fwN(true,2,3,4,5,6,7); + + +# non-variadic functions +gap> f0wN := _JuliaFunction("f0", false);; +gap> f1wN := _JuliaFunction("f1", false);; +gap> f2wN := _JuliaFunction("f2", false);; +gap> f3wN := _JuliaFunction("f3", false);; +gap> f4wN := _JuliaFunction("f4", false);; +gap> f5wN := _JuliaFunction("f5", false);; +gap> f6wN := _JuliaFunction("f6", false);; +gap> f7wN := _JuliaFunction("f7", false);; + +# +gap> f0wN(); + + +# +gap> f1wN(true); + + +# +gap> f2wN(true,2); + + +# +gap> f3wN(true,2,3); + + +# +gap> f4wN(true,2,3,4); + + +# +gap> f5wN(true,2,3,4,5); + + +# +gap> f6wN(true,2,3,4,5,6); + + +# +gap> f7wN(true,2,3,4,5,6,7); + # # calls via wrapped C function pointers @@ -198,32 +279,32 @@ Error, Only 0-6 arguments are supported # gap> g0(); -gap> g1(1); - -gap> g2(1,2); +gap> g1(true); + +gap> g2(true,2); -gap> g3(1,2,3); +gap> g3(true,2,3); -gap> g4(1,2,3,4); +gap> g4(true,2,3,4); -gap> g5(1,2,3,4,5); +gap> g5(true,2,3,4,5); -gap> g6(1,2,3,4,5,6); +gap> g6(true,2,3,4,5,6); # gap> g0C(); -gap> g1C(1); -1 -gap> g2C(1,2); +gap> g1C(true); +true +gap> g2C(true,2); 2 -gap> g3C(1,2,3); +gap> g3C(true,2,3); 3 -gap> g4C(1,2,3,4); +gap> g4C(true,2,3,4); 4 -gap> g5C(1,2,3,4,5); +gap> g5C(true,2,3,4,5); 5 -gap> g6C(1,2,3,4,5,6); +gap> g6C(true,2,3,4,5,6); 6 # @@ -247,17 +328,17 @@ gap> h6C := JuliaBindCFunction("h6", "a,b,c,d,e,f");; # gap> h0C(); Error, TypeError: in h0, in cfunction, expected Ptr{Nothing}, got Nothing -gap> h1C(1); +gap> h1C(true); Error, TypeError: in h1, in cfunction, expected Ptr{Nothing}, got Nothing -gap> h2C(1,2); +gap> h2C(true,2); Error, TypeError: in h2, in cfunction, expected Ptr{Nothing}, got Nothing -gap> h3C(1,2,3); +gap> h3C(true,2,3); Error, TypeError: in h3, in cfunction, expected Ptr{Nothing}, got Nothing -gap> h4C(1,2,3,4); +gap> h4C(true,2,3,4); Error, TypeError: in h4, in cfunction, expected Ptr{Nothing}, got Nothing -gap> h5C(1,2,3,4,5); +gap> h5C(true,2,3,4,5); Error, TypeError: in h5, in cfunction, expected Ptr{Nothing}, got Nothing -gap> h6C(1,2,3,4,5,6); +gap> h6C(true,2,3,4,5,6); Error, TypeError: in h6, in cfunction, expected Ptr{Nothing}, got Nothing # diff --git a/JuliaInterface/tst/utils.tst b/JuliaInterface/tst/utils.tst index 344ead7ed..16e7fd5ba 100644 --- a/JuliaInterface/tst/utils.tst +++ b/JuliaInterface/tst/utils.tst @@ -54,6 +54,38 @@ Error, UndefVarError: This_Module_Does_Not_Exist not defined gap> JuliaModule(1); Error, JuliaModule: must be a string +## +gap> _JuliaGetGlobalVariable(0); +Error, _JuliaGetGlobalVariable: must be a string +gap> _JuliaGetGlobalVariable("not-a-global-variable"); +fail +gap> _JuliaGetGlobalVariable("sqrt"); + + +## +gap> _JuliaGetGlobalVariableByModule(0, 0); +Error, _JuliaGetGlobalVariableByModule: must be a string +gap> _JuliaGetGlobalVariableByModule("sqrt", 0); +Error, _JuliaGetGlobalVariableByModule: must be a string or a Julia m\ +odule +gap> _JuliaGetGlobalVariableByModule("sqrt", Julia.Base.sqrt); +Error, _JuliaGetGlobalVariableByModule: must be a string or a Julia m\ +odule +gap> _JuliaGetGlobalVariableByModule("Base","sqrt"); +Error, Not a module +gap> _JuliaGetGlobalVariableByModule("sqrt","Base"); + +gap> _JuliaGetGlobalVariableByModule("sqrt", JuliaModule("Base")); + + +## +gap> JuliaGetFieldOfObject(1, ""); +Error, JuliaGetFieldOfObject: must be a Julia object +gap> JuliaGetFieldOfObject(JuliaModule("Base"), fail); +Error, JuliaGetFieldOfObject: must be a string +gap> JuliaGetFieldOfObject(JuliaModule("Base"), "not-a-field"); +Error, type Module has no field not-a-field + ## gap> STOP_TEST( "utils.tst", 1 );