Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/cbang/js/StdModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using namespace std;

void StdModule::define(Sink &exports) {
exports.insert("require(id)", this, &StdModule::require);
exports.insert("readfile(path)", this, &StdModule::readfile);
exports.insert("print(...)", this, &StdModule::print);
}

Expand All @@ -48,6 +49,29 @@ SmartPointer<Value> StdModule::require(Callback &cb, Value &args) {
return js.require(cb, args);
}

void test(std::istream& stream) {
printf("---test 1\n");
int c = 0;
while ((c = stream.get()) != EOF) {
printf("--@test 2: %d\n", c);
// printf("--@readfile 3: %d\n", c);
// sink.append(c);
}
}

void StdModule::readfile(const Value &args, Sink &sink) {
// For some reason this needs to be done in two steps or `get` apparently segfaults.
// std::istream& stream = InputSource::open(args.getString("path"));
InputSource file = InputSource::open(args.getString("path"));
std::istream& stream = file;

sink.beginList();
int c = 0;
while ((c = stream.get()) != EOF) {
sink.append(c);
}
sink.endList();
}

void StdModule::print(const Value &args, Sink &sink) {
for (unsigned i = 0; i < args.length(); i++) {
Expand Down
1 change: 1 addition & 0 deletions src/cbang/js/StdModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace cb {

// Callbacks
SmartPointer<Value> require(Callback &cb, Value &args);
void readfile(const Value &args, Sink &sink);
void print(const Value &args, Sink &sink);
};
}
Expand Down