Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

lib: basic array and buffer modules

  • Loading branch information...
commit 2d6a8d984b9e7a93cf1898ad4aa685793ce2e58d 1 parent 7011291
@indutny authored
View
2  candor.io.gyp
@@ -1,7 +1,9 @@
{
'variables': {
'library_files': [
+ 'lib/array.can',
'lib/events.can',
+ 'lib/buffer.can',
'lib/fs.can',
'lib/tcp.can',
'lib/can.can',
View
42 lib/array.can
@@ -0,0 +1,42 @@
+exports = {}
+
+exports.push = (arr, items...) {
+ while (i < sizeof items) {
+ arr[sizeof arr] = items[i++]
+ }
+
+ return sizeof arr
+}
+
+exports.pop = (arr) {
+ index = sizeof arr - 1
+ item = arr[index]
+ delete arr[index]
+
+ return item
+}
+
+exports.slice = (arr, begin, end) {
+ res = []
+ len = sizeof arr
+
+ if (len < begin) return res
+
+ if (!end) end = len
+ i = 0
+ while (begin < end) {
+ res[i++] = arr[begin++]
+ }
+
+ return res
+}
+
+exports.forEach = (arr, cb) {
+ len = sizeof arr
+ while (i < len) {
+ cb(arr[i], i, arr)
+ i++
+ }
+}
+
+return exports
View
1  lib/buffer.can
@@ -0,0 +1 @@
+return global._bindings.buffer
View
13 lib/events.can
@@ -1,5 +1,6 @@
// EventEmitter
exports = {}
+array = global._natives.array
EventEmitter = exports.EventEmitter = {}
@@ -9,18 +10,16 @@ EventEmitter.init = (self) {
EventEmitter.on = (self, event, listener) {
if (!self._listeners[event]) self._listeners[event] = []
- listeners = self._listeners[event]
- listeners[sizeof listeners] = listener
+ array.push(self._listeners[event], listener)
}
EventEmitter.emit = (self, event, args...) {
- i = 0
listeners = self._listeners[event]
- while (i < sizeof listeners) {
- listeners[i](args...)
- i++
- }
+
+ array.forEach(listeners, (listener) {
+ listener(args...)
+ })
return sizeof listeners
}
View
7 src/bindings/buffer.cc
@@ -119,12 +119,19 @@ Value* Buffer::Concat(uint32_t argc, Value** argv) {
}
+Value* Buffer::HasInstance(uint32_t argc, Value** value) {
+ if (argc < 1) return Nil::New();
+ return Boolean::New(HasInstance(value[0]));
+}
+
+
void Buffer::Init(Object* target) {
target->Set("new", Function::New(Buffer::New));
target->Set("length", Function::New(Buffer::Length));
target->Set("stringify", Function::New(Buffer::Stringify));
target->Set("slice", Function::New(Buffer::Slice));
target->Set("concat", Function::New(Buffer::Concat));
+ target->Set("hasInstance", Function::New(Buffer::HasInstance));
}
} // namespace can
View
1  src/bindings/buffer.h
@@ -45,6 +45,7 @@ class Buffer : public candor::CWrapper {
static candor::Value* Stringify(uint32_t argc, candor::Value** argv);
static candor::Value* Slice(uint32_t argc, candor::Value** argv);
static candor::Value* Concat(uint32_t argc, candor::Value** argv);
+ static candor::Value* HasInstance(uint32_t argc, candor::Value** value);
bool allocated_;
char* data_;
Please sign in to comment.
Something went wrong with that request. Please try again.