Skip to content
Browse files

bindings: add source!

  • Loading branch information...
1 parent fe9d51c commit a39a6fcdbddc9416a7bd4365808b8a81549bf045 @indutny committed
Showing with 210 additions and 4 deletions.
  1. +0 −1 .gitignore
  2. +4 −0 lib/can.can
  3. +2 −0 src/bindings.h
  4. +28 −0 src/bindings/buffer.cc
  5. +1 −1 src/bindings/buffer.h
  6. +171 −0 src/bindings/fs.cc
  7. +4 −2 src/bindings/fs.h
View
1 .gitignore
@@ -1,4 +1,3 @@
out/
tools/can2c
-src/bindings/*.cc
/can.io
View
4 lib/can.can
@@ -21,4 +21,8 @@ fs.open("/tmp/1.txt", fs.flags.O_RDWR | fs.flags.O_CREAT, 438, (err, fd1) {
})
})
+fd = fs.openSync("/tmp/3.txt", fs.flags.O_RDWR | fs.flags.O_CREATE, 438)
+fs.closeSync(fd)
+log(global._bindings.buffer.new(1024))
+
return { yay: true }
View
2 src/bindings.h
@@ -2,6 +2,7 @@
#define _SRC_BINDINGS_H
#include "bindings/fs.h"
+#include "bindings/buffer.h"
#include <candor.h>
namespace can {
@@ -13,6 +14,7 @@ struct can_binding_s {
static const struct can_binding_s can_bindings[] = {
{ "fs", FS::Init },
+ { "buffer", Buffer::Init },
{ NULL, NULL }
};
View
28 src/bindings/buffer.cc
@@ -0,0 +1,28 @@
+#include "buffer.h"
+
+namespace can {
+
+using namespace candor;
+
+const int Buffer::magic = 0;
+
+Value* Buffer::New(uint32_t argc, Value** argv) {
+ if (argc < 1) return Nil::New();
+
+ Buffer* b = NULL;
+
+ if (argv[0]->Is<Number>()) {
+ b = new Buffer(argv[0]->As<Number>()->IntegralValue());
+ } else {
+ return Nil::New();
+ }
+
+ return b->Wrap();
+}
+
+
+void Buffer::Init(Object* target) {
+ target->Set("new", Function::New(Buffer::New));
+}
+
+} // namespace can
View
2 src/bindings/buffer.h
@@ -21,7 +21,7 @@ class Buffer : public candor::CWrapper {
inline char* data() { return data_; }
inline size_t size() { return size_; }
- static void New(uint32_t argc, candor::Value** argv);
+ static candor::Value* New(uint32_t argc, candor::Value** argv);
static void Init(candor::Object* target);
// Magic word
View
171 src/bindings/fs.cc
@@ -0,0 +1,171 @@
+#include "bindings/fs.h"
+
+#include <stdlib.h> // NULL
+#include <string.h> // memcpy
+#include <fcntl.h> // O_RDONLY and everything
+#include <candor.h>
+#include <uv.h>
+
+namespace can {
+
+using namespace candor;
+
+const int FSWrap::magic = 0;
+
+FSWrap::FSWrap(Function* cb) : CWrapper(&magic), cb_(cb) {
+ req_ = new uv_fs_t();
+ req_->data = this;
+ Ref();
+}
+
+
+FSWrap::~FSWrap() {
+ cb_.Unref();
+ if (req_ == NULL) return;
+ delete req_;
+}
+
+
+void FSWrap::HandleCallback(uv_fs_t* req) {
+ FSWrap* wrap = reinterpret_cast<FSWrap*>(req->data);
+
+ Value* argv[2] = { Nil::New(), Number::NewIntegral(req->result) };
+ wrap->cb_->Call(2, argv);
+ wrap->cb_.Unref();
+ wrap->Unref();
+}
+
+
+void FSWrap::HandleClose(uv_handle_t* handle) {
+ delete handle;
+}
+
+
+char* StringToChar(Value* value) {
+ String* str = value->As<String>();
+
+ char* res = new char[str->Length() + 1];
+ memcpy(res, str->Value(), str->Length());
+ res[str->Length()] = 0;
+
+ return res;
+}
+
+
+Value* FS::Open(uint32_t argc, Value** argv) {
+ // Input constraints
+ if (argc < 4 ||
+ !argv[0]->Is<String>() ||
+ !argv[1]->Is<Number>() ||
+ !argv[2]->Is<Number>() ||
+ !argv[3]->Is<Function>()) {
+ return Nil::New();
+ }
+
+ Function* cb = argv[3]->As<Function>();
+ FSWrap* wrap = new FSWrap(cb);
+
+ char* path = StringToChar(argv[0]);
+ int r = uv_fs_open(uv_default_loop(),
+ wrap->req(),
+ path,
+ argv[1]->As<Number>()->IntegralValue(),
+ argv[2]->As<Number>()->IntegralValue(),
+ FSWrap::HandleCallback);
+ delete[] path;
+
+ if (r) {
+ Value* argv[2] = { String::New("Request failed"), Number::NewIntegral(r) };
+ cb->Call(2, argv);
+ }
+
+ return Nil::New();
+}
+
+
+Value* FS::OpenSync(uint32_t argc, Value** argv) {
+ // Input constraints
+ if (argc < 3 ||
+ !argv[0]->Is<String>() ||
+ !argv[1]->Is<Number>() ||
+ !argv[2]->Is<Number>()) {
+ return Nil::New();
+ }
+
+ uv_fs_t req;
+ char* path = StringToChar(argv[0]);
+ int r = uv_fs_open(uv_default_loop(),
+ &req,
+ path,
+ argv[1]->As<Number>()->IntegralValue(),
+ argv[2]->As<Number>()->IntegralValue(),
+ NULL);
+ delete[] path;
+
+ return Number::NewIntegral(r);
+}
+
+
+Value* FS::Close(uint32_t argc, Value** argv) {
+ // Input constraints
+ if (argc < 2 || !argv[0]->Is<Number>() || !argv[1]->Is<Function>()) {
+ return Nil::New();
+ }
+
+ Function* cb = argv[1]->As<Function>();
+ FSWrap* wrap = new FSWrap(cb);
+
+ int r = uv_fs_close(uv_default_loop(),
+ wrap->req(),
+ argv[0]->As<Number>()->IntegralValue(),
+ FSWrap::HandleCallback);
+
+ if (r) {
+ Value* argv[2] = { String::New("Request failed"), Number::NewIntegral(r) };
+ cb->Call(2, argv);
+ }
+
+ return Nil::New();
+}
+
+
+Value* FS::CloseSync(uint32_t argc, Value** argv) {
+ // Input constraints
+ if (argc < 1 || !argv[0]->Is<Number>()) {
+ return Nil::New();
+ }
+
+ uv_fs_t req;
+ int r = uv_fs_close(uv_default_loop(),
+ &req,
+ argv[0]->As<Number>()->IntegralValue(),
+ NULL);
+
+ return Number::NewIntegral(r);
+}
+
+
+Value* FS::Read(uint32_t argc, Value** argv) {
+ return Nil::New();
+}
+
+
+void FS::Init(Object* target) {
+ // Exports flags
+ Object* flags = Object::New();
+ flags->Set("O_RDONLY", Number::NewIntegral(O_RDONLY));
+ flags->Set("O_WRONLY", Number::NewIntegral(O_WRONLY));
+ flags->Set("O_CREAT", Number::NewIntegral(O_CREAT));
+ flags->Set("O_APPEND", Number::NewIntegral(O_APPEND));
+ flags->Set("O_RDWR", Number::NewIntegral(O_RDWR));
+ target->Set("flags", flags);
+
+ // Exports functions
+ target->Set("open", Function::New(Open));
+ target->Set("openSync", Function::New(OpenSync));
+ target->Set("close", Function::New(Close));
+ target->Set("closeSync", Function::New(CloseSync));
+ target->Set("read", Function::New(Read));
+}
+
+} // namespace can
View
6 src/bindings/fs.h
@@ -15,13 +15,13 @@ class FSWrap : public candor::CWrapper {
inline uv_fs_t* req() { return req_; }
+ static const int magic;
+
protected:
static void HandleClose(uv_handle_t* handle);
uv_fs_t* req_;
candor::Handle<candor::Function> cb_;
-
- static const int magic;
};
class FS {
@@ -30,7 +30,9 @@ class FS {
protected:
static candor::Value* Open(uint32_t argc, candor::Value** argv);
+ static candor::Value* OpenSync(uint32_t argc, candor::Value** argv);
static candor::Value* Close(uint32_t argc, candor::Value** argv);
+ static candor::Value* CloseSync(uint32_t argc, candor::Value** argv);
static candor::Value* Read(uint32_t argc, candor::Value** argv);
};

0 comments on commit a39a6fc

Please sign in to comment.
Something went wrong with that request. Please try again.