Skip to content

Commit

Permalink
Start restructuring API into JRuby and MRI versions
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 6, 2024
1 parent 553bae8 commit c9c3e12
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 57 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2435,23 +2435,23 @@ public IRubyObject close_on_exec_set(ThreadContext context, IRubyObject arg) {
if (this != write_io) {
fptr = write_io.getOpenFileChecked();
if (fptr != null && 0 <= (fd = fptr.fd().realFileno)) {
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) return API.rb_sys_fail_path(runtime, fptr.getPath());
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) return API.sysFailWithPath(context, fptr.getPath());
if ((ret & FD_CLOEXEC) != flag) {
ret = (ret & ~FD_CLOEXEC) | flag;
ret = posix.fcntlInt(fd, Fcntl.F_SETFD, ret);
if (ret == -1) API.rb_sys_fail_path(runtime, fptr.getPath());
if (ret == -1) API.sysFailWithPath(context, fptr.getPath());
}
}

}

fptr = getOpenFileChecked();
if (fptr != null && 0 <= (fd = fptr.fd().realFileno)) {
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) API.rb_sys_fail_path(runtime, fptr.getPath());
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) API.sysFailWithPath(context, fptr.getPath());
if ((ret & FD_CLOEXEC) != flag) {
ret = (ret & ~FD_CLOEXEC) | flag;
ret = posix.fcntlInt(fd, Fcntl.F_SETFD, ret);
if (ret == -1) API.rb_sys_fail_path(runtime, fptr.getPath());
if (ret == -1) API.sysFailWithPath(context, fptr.getPath());
}
}

Expand All @@ -2477,14 +2477,14 @@ public IRubyObject close_on_exec_p(ThreadContext context) {
if (this != write_io) {
fptr = write_io.getOpenFileChecked();
if (fptr != null && 0 <= (fd = fptr.fd().realFileno)) {
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) API.rb_sys_fail_path(runtime, fptr.getPath());
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) API.sysFailWithPath(context, fptr.getPath());
if ((ret & FD_CLOEXEC) == 0) return context.fals;
}
}

fptr = getOpenFileChecked();
if (fptr != null && 0 <= (fd = fptr.fd().realFileno)) {
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) API.rb_sys_fail_path(runtime, fptr.getPath());
if ((ret = posix.fcntl(fd, Fcntl.F_GETFD)) == -1) API.sysFailWithPath(context, fptr.getPath());
if ((ret & FD_CLOEXEC) == 0) return context.fals;
}
return context.tru;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ private boolean coverRange(ThreadContext context, RubyRange val) {

IRubyObject nil = context.nil;

IRubyObject valMax = API.rb_rescue_typeerror(context, nil, () -> sites(context).max.call(context, this, val));
IRubyObject valMax = API.rescueTypeError(context, nil, () -> sites(context).max.call(context, this, val));

if (valMax == nil) return false;

Expand Down
95 changes: 49 additions & 46 deletions core/src/main/java/org/jruby/api/API.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jruby.api;

import org.jruby.Ruby;
import org.jruby.RubyIO;
import org.jruby.exceptions.TypeError;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
Expand All @@ -11,66 +10,70 @@
import java.util.function.Supplier;

public class API {
public static IRubyObject rb_sys_fail_path(Ruby runtime, String path) {
throw runtime.newSystemCallError("bad path for cloexec: " + path);
/**
* Equivalent to {@link MRI#rb_sys_fail_path(ThreadContext, String)}.
*
* @param context
* @param path
* @return
*/
public static IRubyObject sysFailWithPath(ThreadContext context, String path) {
throw context.runtime.newSystemCallError("bad path for cloexec: " + path);
}

public static int rb_pipe(Ruby runtime, int[] pipes) {
/**
* Equivalent to {@link MRI#rb_pipe(Ruby, int[])}.
*
* @param context
* @param pipes
* @return
*/
public static int newPipe(ThreadContext context, int[] pipes) {
int ret;
ret = rb_cloexec_pipe(runtime, pipes);
// if (ret == -1) {
// if (rb_gc_for_fd(errno)) {
// ret = rb_cloexec_pipe(pipes);
// }
// }
// if (ret == 0) {
// rb_update_max_fd(pipes[0]);
// rb_update_max_fd(pipes[1]);
// }
ret = cloexecPipe(context, pipes);
return ret;
}

public static int rb_cloexec_pipe(Ruby runtime, int[] pipes) {
/**
* Equivalent to {@link MRI#rb_cloexec_pipe(Ruby, int[])}.
*
* @param context
* @param pipes
* @return
*/
public static int cloexecPipe(ThreadContext context, int[] pipes) {
int ret;

//#if defined(HAVE_PIPE2)
// static int try_pipe2 = 1;
// if (try_pipe2) {
// ret = runtime.posix.pippipe2(fildes, O_CLOEXEC);
// if (ret != -1)
// return ret;
// /* pipe2 is available since Linux 2.6.27, glibc 2.9. */
// if (errno == ENOSYS) {
// try_pipe2 = 0;
// ret = pipe(fildes);
// }
// }
// else {
// ret = pipe(fildes);
// }
//#else
ret = runtime.getPosix().pipe(pipes);
//#endif
ret = context.runtime.getPosix().pipe(pipes);

if (ret == -1) return -1;
//#ifdef __CYGWIN__
// if (ret == 0 && fildes[1] == -1) {
// close(fildes[0]);
// fildes[0] = -1;
// errno = ENFILE;
// return -1;
// }
//#endif
rb_maygvl_fd_fix_cloexec(runtime, pipes[0]);
rb_maygvl_fd_fix_cloexec(runtime, pipes[1]);

fdFixCloexec(context, pipes[0]);
fdFixCloexec(context, pipes[1]);
return ret;
}

public static void rb_maygvl_fd_fix_cloexec(Ruby runtime, int fd) {
PosixShim shim = new PosixShim(runtime);
/**
* Equivalent to {@link MRI#rb_maygvl_fd_fix_cloexec(Ruby, int)}.
*
* @param context
* @param fd
*/
public static void fdFixCloexec(ThreadContext context, int fd) {
PosixShim shim = new PosixShim(context.runtime);
OpenFile.fdFixCloexec(shim, fd);
}

public static <T> T rb_rescue_typeerror(ThreadContext context, T dflt, Supplier<T> func) {
/**
* Equivalent to {@link MRI#rb_rescue_typeerror(ThreadContext, Object, Supplier)}.
*
* @param context
* @param dflt
* @param func
* @return
* @param <T>
*/
public static <T> T rescueTypeError(ThreadContext context, T dflt, Supplier<T> func) {
boolean exceptionRequiresBacktrace = context.exceptionRequiresBacktrace;
try {
context.setExceptionRequiresBacktrace(false);
Expand Down
28 changes: 28 additions & 0 deletions core/src/main/java/org/jruby/api/MRI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jruby.api;

import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.function.Supplier;

public class MRI {
public static IRubyObject rb_sys_fail_path(ThreadContext context, String path) {
return API.sysFailWithPath(context, path);
}

public static int rb_pipe(ThreadContext context, int[] pipes) {
return API.newPipe(context, pipes);
}

public static int rb_cloexec_pipe(ThreadContext context, int[] pipes) {
return API.cloexecPipe(context, pipes);
}

public static void rb_maygvl_fd_fix_cloexec(ThreadContext context, int fd) {
API.fdFixCloexec(context, fd);
}

public static <T> T rb_rescue_typeerror(ThreadContext context, T dflt, Supplier<T> func) {
return API.rescueTypeError(context, dflt, func);
}
}
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/util/io/PopenExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ private RubyIO pipeOpen(ThreadContext context, ExecArg eargp, String modestr, in
int[] pair = {-1,-1}, writePair = {-1, -1};
switch (fmode & (OpenFile.READABLE|OpenFile.WRITABLE)) {
case OpenFile.READABLE | OpenFile.WRITABLE:
if (API.rb_pipe(runtime, writePair) == -1)
if (API.newPipe(context, writePair) == -1)
throw runtime.newErrnoFromErrno(posix.getErrno(), prog.toString());
if (API.rb_pipe(runtime, pair) == -1) {
if (API.newPipe(context, pair) == -1) {
e = posix.getErrno();
runtime.getPosix().close(writePair[1]);
runtime.getPosix().close(writePair[0]);
Expand All @@ -615,14 +615,14 @@ private RubyIO pipeOpen(ThreadContext context, ExecArg eargp, String modestr, in

break;
case OpenFile.READABLE:
if (API.rb_pipe(runtime, pair) == -1)
if (API.newPipe(context, pair) == -1)
throw runtime.newErrnoFromErrno(posix.getErrno(), prog.toString());

if (eargp != null) prepareStdioRedirects(runtime, pair, null, eargp);

break;
case OpenFile.WRITABLE:
if (API.rb_pipe(runtime, pair) == -1)
if (API.newPipe(context, pair) == -1)
throw runtime.newErrnoFromErrno(posix.getErrno(), prog.toString());

if (eargp != null) prepareStdioRedirects(runtime, null, pair, eargp);
Expand Down

0 comments on commit c9c3e12

Please sign in to comment.