void FileObject::open(gc<Fiber> fiber, gc<String> path) { FSTask* task = new FSTask(fiber); // TODO(bob): Make this configurable. int flags = O_RDONLY; // TODO(bob): Make this configurable when creating a file. int mode = 0; uv_fs_open(task->loop(), task->request(), path->cString(), flags, mode, openFileCallback); }
void Compiler::declareVariable(gc<SourcePos> pos, gc<String> name, Module* module) { // Make sure there isn't already a top-level variable with that name. int existing = module->findVariable(name); if (existing != -1) { reporter_.error(pos, "There is already a variable '%s' defined in this module.", name->cString()); } module->addVariable(name, gc<Object>()); }
gc<ResolvedName> Resolver::makeLocal(gc<SourcePos> pos, gc<String> name) { // Make sure there isn't already a local variable with this name in the // current scope. for (int i = scope_->startSlot(); i < locals_.count(); i++) { if (locals_[i].name() == name) { compiler_.reporter().error(pos, "There is already a variable '%s' defined in this scope.", name->cString()); } } gc<ResolvedName> resolved = new ResolvedName(locals_.count()); locals_.add(Local(name, resolved)); if (locals_.count() > maxLocals_) { maxLocals_ = locals_.count(); } return resolved; }
// Reads a file from the given path into a String. gc<String> readFile(gc<String> path) { // TODO(bob): Use platform-native API for this? using namespace std; ifstream stream(path->cString()); if (stream.fail()) return gc<String>(); // From: http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring. string str; // Allocate a std::string big enough for the file. stream.seekg(0, ios::end); str.reserve(stream.tellg()); stream.seekg(0, ios::beg); // Read it in. str.assign((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>()); return String::create(str.c_str()); }
void SignatureBuilder::add(gc<String> text) { add(text->cString()); }
gc<String> join(gc<String> a, gc<String> b) { // TODO(bob): Handle lots of edge cases better here. return String::format("%s%c%s", a->cString(), separator(), b->cString()); }
bool fileExists(gc<String> path) { // If we can stat it, it exists. struct stat dummy; return stat(path->cString(), &dummy) == 0; }
gc<String> real(gc<String> path) { char absolute[PATH_MAX]; realpath(path->cString(), absolute); return String::create(absolute); }