Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add nqp::getpid for both JVM and Parrot.
  • Loading branch information
jnthn committed Jun 23, 2013
1 parent 060b4b4 commit f3cab58
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -2316,6 +2316,7 @@ QAST::OperationsJAST.map_classlib_core_op('nfarunalt', $TYPE_OPS, 'nfarunalt', [
QAST::OperationsJAST.map_classlib_core_op('exit', $TYPE_OPS, 'exit', [$RT_INT], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('sleep', $TYPE_OPS, 'sleep', [$RT_NUM], $RT_NUM);
QAST::OperationsJAST.map_classlib_core_op('getenvhash', $TYPE_OPS, 'getenvhash', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('getpid', $TYPE_OPS, 'getpid', [], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('jvmgetproperties', $TYPE_OPS, 'jvmgetproperties', [], $RT_OBJ, :tc);

# JVM-specific ops for compilation unit handling
Expand Down
18 changes: 18 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -3400,6 +3400,24 @@ public static SixModelObject getenvhash(ThreadContext tc) {

return res;
}

public static long getpid(ThreadContext tc) {
/* Questionably portable; see:
* http://boxysystems.com/index.php/java-tip-find-process-id-of-running-java-process/
*/
try {
java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
jvm.setAccessible(true);
sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
pid_method.setAccessible(true);
return (Integer)pid_method.invoke(mgmt);
}
catch (Throwable t) {
throw ExceptionHandling.dieInternal(tc, t);
}
}

public static SixModelObject jvmgetproperties(ThreadContext tc) {
SixModelObject hashType = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.hashType;
Expand Down
11 changes: 10 additions & 1 deletion src/vm/parrot/QAST/Operations.nqp
Expand Up @@ -2284,10 +2284,19 @@ QAST::Operations.add_core_pirop_mapping('exit', 'exit', '0i', :inlinable(1));
QAST::Operations.add_core_pirop_mapping('sleep', 'sleep', '0n', :inlinable(1));
QAST::Operations.add_core_op('getenvhash', -> $qastcomp, $op {
if +@($op) != 0 {
nqp::die('getenvhash requires three operands');
nqp::die('getenvhash requires no operands');
}
$qastcomp.as_post(QAST::VM.new(
:pirop('new__Ps'),
QAST::SVal.new( :value('Env') )
))
});
QAST::Operations.add_core_op('getpid', -> $qastcomp, $op {
if +@($op) != 0 {
nqp::die('getenvhash requires no operands');
}
$qastcomp.as_post(QAST::Op.new(
:op('callmethod'), :name('getpid'),
QAST::VM.new( :pirop('getinterp__P') )
))
});

0 comments on commit f3cab58

Please sign in to comment.