Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Build NativeCallInstance.{arg_types,ret_type} in nqp::buildnativecall.
  • Loading branch information
arnsholt committed Sep 9, 2013
1 parent 518a292 commit a83bf2b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -2462,8 +2462,8 @@ QAST::OperationsJAST.map_classlib_core_op('jvmgetconfig', $TYPE_OPS, 'jvmgetconf

# Native call ops
QAST::OperationsJAST.map_classlib_core_op('initnativecall', $TYPE_NATIVE_OPS, 'init', [], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('buildnativecall', $TYPE_NATIVE_OPS, 'build', [$RT_OBJ, $RT_STR, $RT_STR, $RT_STR, $RT_OBJ, $RT_OBJ], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('nativecall', $TYPE_NATIVE_OPS, 'call', [$RT_OBJ, $RT_OBJ, $RT_OBJ], $RT_OBJ);
QAST::OperationsJAST.map_classlib_core_op('buildnativecall', $TYPE_NATIVE_OPS, 'build', [$RT_OBJ, $RT_STR, $RT_STR, $RT_STR, $RT_OBJ, $RT_OBJ], $RT_INT, :tc);

class QAST::CompilerJAST {
# Responsible for handling issues around code references, building the
Expand Down
28 changes: 27 additions & 1 deletion src/vm/jvm/runtime/org/perl6/nqp/runtime/NativeCallOps.java
Expand Up @@ -5,6 +5,7 @@
import org.perl6.nqp.sixmodel.SixModelObject;

import org.perl6.nqp.sixmodel.reprs.NativeCallInstance;
import org.perl6.nqp.sixmodel.reprs.NativeCallInstance.ArgType;

public final class NativeCallOps {
public static long init() {
Expand All @@ -13,7 +14,7 @@ public static long init() {
return 1L;
}

public static long build(SixModelObject target, String libname, String symbol, String convention, SixModelObject arguments, SixModelObject returns) {
public static long build(SixModelObject target, String libname, String symbol, String convention, SixModelObject arguments, SixModelObject returns, ThreadContext tc) {
NativeCallInstance call = getNativeCallInstance(target);

/* Load the library and locate the symbol. */
Expand All @@ -24,6 +25,13 @@ public static long build(SixModelObject target, String libname, String symbol, S
/* TODO: Set the calling convention. */

/* Set up the argument types. */
int n = (int) arguments.elems(tc);
call.arg_types = new ArgType[n];
for (int i = 0; i < n; i++) {
call.arg_types[i] = getArgType(tc, arguments.at_pos_boxed(tc, i), false);
}

call.ret_type = getArgType(tc, returns, true);

return 1L;
}
Expand All @@ -43,4 +51,22 @@ private static NativeCallInstance getNativeCallInstance(SixModelObject target) {
}
return call;
}

private static ArgType getArgType(ThreadContext tc, SixModelObject info, boolean isReturn) {
String type_name = info.at_key_boxed(tc, "type").get_str(tc);

ArgType type = ArgType.VOID;
try {
type = ArgType.valueOf(ArgType.class, type_name.toUpperCase());
}
catch (IllegalArgumentException e) {
ExceptionHandling.dieInternal(tc, String.format("Unknown type '%s' used for native call", type_name));
}

if (!isReturn && type == ArgType.VOID) {
ExceptionHandling.dieInternal(tc, "Can only use 'void' type on native call return values");
}

return type;
}
}

0 comments on commit a83bf2b

Please sign in to comment.