Skip to content

Commit 9fcd736

Browse files
committed
Add gethostname op
1 parent fd1de52 commit 9fcd736

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,8 @@ QAST::OperationsJAST.map_classlib_core_op('rename', $TYPE_OPS, 'rename', [$RT_ST
19211921
QAST::OperationsJAST.map_classlib_core_op('copy', $TYPE_OPS, 'copy', [$RT_STR, $RT_STR], $RT_INT, :tc);
19221922
QAST::OperationsJAST.map_classlib_core_op('link', $TYPE_OPS, 'link', [$RT_STR, $RT_STR], $RT_INT, :tc);
19231923

1924+
QAST::OperationsJAST.map_classlib_core_op('gethostname', $TYPE_OPS, 'gethostname', [], $RT_STR);
1925+
19241926
# Two variants of shell until we deprecate shell1
19251927
QAST::OperationsJAST.map_classlib_core_op('shell1', $TYPE_OPS, 'shell1', [$RT_STR], $RT_INT, :tc);
19261928
QAST::OperationsJAST.map_classlib_core_op('shell3', $TYPE_OPS, 'shell3', [$RT_STR, $RT_STR, $RT_OBJ], $RT_INT, :tc);

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Set;
3535
import java.util.concurrent.LinkedBlockingQueue;
3636
import java.util.concurrent.TimeUnit;
37+
import java.net.InetAddress;
3738

3839
import org.perl6.nqp.io.AsyncFileHandle;
3940
import org.perl6.nqp.io.FileHandle;
@@ -841,6 +842,14 @@ public static SixModelObject openpipe(String cmd, String dir,
841842
return h;
842843
}
843844

845+
public static String gethostname(){
846+
try {
847+
String hostname = InetAddress.getLocalHost().getHostName();
848+
return hostname;
849+
} catch (Exception e) {
850+
return null;
851+
}
852+
}
844853

845854
// To be removed once shell3 is adopted
846855
public static long shell1(String cmd, ThreadContext tc) {

src/vm/parrot/QAST/Operations.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,7 @@ QAST::Operations.add_core_pirop_mapping('nfarunalt', 'nqp_nfa_run_alt', '0PsiPPP
25132513
# process related opcodes
25142514
QAST::Operations.add_core_pirop_mapping('exit', 'exit', '0i', :inlinable(1));
25152515
QAST::Operations.add_core_pirop_mapping('sleep', 'sleep', '0n', :inlinable(1));
2516+
QAST::Operations.add_core_pirop_mapping('gethostname', 'nqp_gethostname', 'S');
25162517
QAST::Operations.add_core_pirop_mapping('shell', 'nqp_shell', 'IssP');
25172518
QAST::Operations.add_core_pirop_mapping('getenvhash', 'nqp_getenvhash', 'P');
25182519

src/vm/parrot/ops/nqp.ops

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,6 +3539,12 @@ inline op nqp_delete_f(out INT, in STR) :base_core {
35393539
#endif
35403540
}
35413541

3542+
inline op nqp_gethostname(out STR) {
3543+
char hostname[65];
3544+
gethostname(hostname, 65);
3545+
$1 = Parrot_str_new_init(interp, hostname, strlen(hostname), Parrot_utf8_encoding_ptr, 0);
3546+
}
3547+
35423548
inline op nqp_shell(out INT, in STR, in STR, in PMC) {
35433549
STRING *command = $2;
35443550
STRING *dir = $3;

0 commit comments

Comments
 (0)