@BIF public static ESeq reverse(EObject hd, EObject tl) { ESeq res = tl.testSeq(); ESeq front = hd.testSeq(); if (res == null) throw ERT.badarg(hd, tl); return reverse(front, res); }
@BIF public static EObject keysearch(EObject k, EObject n, EObject list) { ESmall idx = n.testSmall(); ESeq src = list.testSeq(); if (k == null || idx == null || src == null || idx.value < 1) throw ERT.badarg(k, n, list); int index = idx.value; while (!src.isNil()) { EObject elm = src.head(); ETuple tup; if ((tup = elm.testTuple()) != null) { if (tup.arity() >= index) { if (tup.elm(index).equals(k)) { return new ETuple2(ERT.am_value, tup); } } } src = src.tail(); } return ERT.FALSE; }
@BIF public static EAtom member(EObject e, EObject l) { ESeq list = l.testSeq(); if (list == null) throw ERT.badarg(e, l); while (!list.isNil()) { if (e.equals(list.head())) return ERT.TRUE; list = list.tail(); } return ERT.FALSE; }
@BIF public static EObject hibernate(EProc self, EObject a1, EObject a2, EObject a3) { EAtom m = a1.testAtom(); EAtom f = a2.testAtom(); ESeq a = a3.testSeq(); if (m == null || f == null || a == null) { throw ERT.badarg(a1, a2, a3); } int arity = a.length(); EFun target = EModuleManager.resolve(new FunID(m, f, arity)); if (target == null) { throw new ErlangUndefined(m, f, new ESmall(arity)); } self.tail = target; a = a.reverse(); switch (arity) { default: throw new NotImplemented("hibernate w/" + arity + " args"); case 7: self.arg6 = a.head(); a = a.tail(); case 6: self.arg5 = a.head(); a = a.tail(); case 5: self.arg4 = a.head(); a = a.tail(); case 4: self.arg3 = a.head(); a = a.tail(); case 3: self.arg2 = a.head(); a = a.tail(); case 2: self.arg1 = a.head(); a = a.tail(); case 1: self.arg0 = a.head(); // a = a.tail(); case 0: } throw ErjangHibernateException.INSTANCE; }
@BIF public static EObject spawn(EProc proc, EObject mod, EObject fun, EObject args) { EAtom m = mod.testAtom(); EAtom f = fun.testAtom(); ESeq a = args.testSeq(); if (m == null || f == null || a == null) throw ERT.badarg(mod, fun, args); EProc p2 = new EProc(proc.group_leader(), m, f, a); ERT.run(p2); return p2.self_handle(); }
@BIF public static ESeq map(EProc proc, EObject f, EObject s) throws Pausable { EFun fun = f.testFunction2(1); ESeq seq = s.testSeq(); if (fun == null || seq == null) throw ERT.badarg(f, s); EObject[] arg = new EObject[1]; ESeq rev = ERT.NIL; for (; !seq.isNil(); seq = seq.tail()) { arg[0] = seq.head(); rev = rev.cons(fun.invoke(proc, arg)); } return reverse(rev, ERT.NIL); }
public static EObject demonitor(ETask<?> self, EObject ref, EObject options) throws Pausable { ERef r = ref.testReference(); ESeq o = options.testSeq(); if (r == null || o == null) throw ERT.badarg(ref, options); boolean flush = (!o.isNil() && o.head() == am_flush); EObject found = self.demonitor(r); if (found == null) { return ERT.FALSE; } EHandle h; ETuple tup; EAtom name; EAtom node; if ((h = found.testHandle()) != null) { h.remove_monitor(self.self_handle(), r, flush); } else if ((tup = found.testTuple()) != null && tup.arity() == 2 && (name = tup.elm(1).testAtom()) != null && (node = tup.elm(2).testAtom()) != null) { EAbstractNode n = EAbstractNode.get_or_connect(self, node); if (n != null) { n.dsig_demonitor(self.self_handle(), r, name); } } if (flush && (self instanceof EProc)) { flush_monitor_message.invoke((EProc) self, new EObject[] {ref, ERT.am_ok}); } return ERT.TRUE; }
@BIF public static EObject keyfind(EObject key, EObject nth_arg, EObject list_arg) { ESmall nth = nth_arg.testSmall(); ESeq list = list_arg.testSeq(); if (key == null || nth == null | list == null) throw ERT.badarg(key, nth_arg, list_arg); while (!list.isNil()) { EObject elm = list.head(); ETuple tup = elm.testTuple(); // test that it is a tuple of the right size if (tup != null && tup.arity() >= nth.value) { EObject val = tup.elm(nth.value); if (val.equals(key)) { return tup; } } list = list.tail(); } return ERT.FALSE; }