@Signature public static String nameNoExt(String path) { String name = new File(path).getName(); int indexOf = name.lastIndexOf('.'); if (indexOf > -1) { name = name.substring(0, indexOf); } return name; }
@Signature public static String abs(String path) { try { return new File(path).getCanonicalPath(); } catch (IOException e) { return new File(path).getAbsolutePath(); } }
@Override @Signature(@Arg("offset")) synchronized public Memory offsetUnset(Environment env, Memory... args) { userEnv.remove(args[0].toString()); return Memory.NULL; }
@Signature(@Arg("size")) public Memory truncate(Environment env, Memory... args) throws IOException { accessFile.setLength(args[0].toLong()); return Memory.NULL; } }
@Signature({@Arg("value"), @Arg(value = "length", optional = @Optional("NULL"))}) public Memory write(Environment env, Memory... args){ int len = args[1].toInteger(); byte[] bytes = args[0].getBinaryBytes(env.getDefaultCharset()); try { accessFile.write(bytes, 0, len == 0 ? bytes.length : len); return LongMemory.valueOf(len == 0 ? bytes.length : len); } catch (IOException e) { env.exception(WrapIOException.class, e.getMessage()); } return Memory.FALSE; }
@Signature public Memory length(Environment env, Memory... args) { try { return LongMemory.valueOf(file.length()); } catch (Exception e){ return Memory.FALSE; } }
@Signature public Memory getParentFile(Environment env, Memory... args){ if (file.getParentFile() == null) return Memory.NULL; FileObject fo = new FileObject(env, __class__); fo.file = file.getParentFile(); return new ObjectMemory(fo); }
@Signature public Memory getCanonicalFile(Environment env, Memory... args){ FileObject fo = new FileObject(env, __class__); try { fo.file = file.getCanonicalFile(); } catch (IOException e) { exception(env, e.getMessage()); return Memory.NULL; } return new ObjectMemory(fo); }
@Signature public Memory __debugInfo(Environment env, Memory... args) { ArrayMemory r = new ArrayMemory(); r.refOfIndex("*path").assign(file.getPath()); return r.toConstant(); }
@Signature public Memory wait(Environment env) { while (this.state == State.PENDING) { // loop. } if (this.state == State.FULFILLED) return this.value; if (this.state == State.REJECTED) { env.__throwException(this.value.toObject(BaseBaseException.class)); } return Memory.NULL; }
@Name("catch") @Signature public WrapPromise _catch(Environment env, @Nullable @Optional("null") Invoker onRejected) throws Exception { return this.then(env, null, onRejected); }
@Signature public static Memory createCached(Environment env, Memory... args){ return new ObjectMemory(new WrapThreadPool(env, Executors.newCachedThreadPool() )); }
@Signature public Memory shutdownNow(Environment env, Memory... args){ service.shutdownNow(); return Memory.NULL; }
@Signature public Memory getClasses() { ArrayMemory result = ArrayMemory.createHashed(); for (NamespaceRecord record : namespaces.values()) { Collection<ClassRecord> classes = record.getClasses(); for (ClassRecord aClass : classes) { result.put(aClass.getName(), aClass); } } return result.toConstant(); }
@Signature synchronized public Memory set(String key, Memory value, boolean override) { if (override || map.get(key) == null) { Memory result = map.put(key, value); return result == null ? Memory.UNDEFINED : result; } return Memory.NULL; }
@Signature synchronized public Memory get(String key, Memory defaultValue) { Memory result = map.get(key); return result == null ? defaultValue : result; }
@Override @Signature synchronized public boolean isEmpty() { return map.isEmpty(); }
@Signature synchronized public Memory __debugInfo(Environment env, Memory... args) { ArrayMemory info = new ArrayMemory(); info.refOfIndex("*map").assign(ArrayMemory.ofMap(map)); return info.toConstant(); }
@Signature synchronized public Memory __debugInfo(Environment env, Memory... args) { ArrayMemory info = new ArrayMemory(); info.refOfIndex("*stack").assign(ArrayMemory.ofCollection(stack)); return info.toConstant(); }
@Signature synchronized public Memory __debugInfo(Environment env, Memory... args) { ArrayMemory info = new ArrayMemory(); info.refOfIndex("*queue").assign(ArrayMemory.ofCollection(queue)); return info.toConstant(); }