Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created July 6, 2013 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/5938207 to your computer and use it in GitHub Desktop.
Save isaacs/5938207 to your computer and use it in GitHub Desktop.
$ vim src/tcp_wrap.cc
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "node.h"
#include "node_buffer.h"
#include "req_wrap.h"
#include "handle_wrap.h"
#include "stream_wrap.h"
#include "tcp_wrap.h"
#include <stdlib.h>
namespace node {
using v8::Arguments;
using v8::Context;
using v8::Function;
using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::Persistent;
using v8::PropertyAttribute;
using v8::String;
using v8::TryCatch;
using v8::Undefined;
using v8::Value;
static Persistent<Function> tcpConstructor;
static Persistent<String> oncomplete_sym;
static Persistent<String> onconnection_sym;
typedef class ReqWrap<uv_connect_t> ConnectWrap;
Local<Object> AddressToJS(const sockaddr* addr);
Local<Object> TCPWrap::Instantiate() {
// If this assert fire then process.binding('tcp_wrap') hasn't been
// called yet.
assert(tcpConstructor.IsEmpty() == false);
HandleScope scope;
Local<Object> obj = tcpConstructor->NewInstance();
using v8::Value;
static Persistent<Function> tcpConstructor;
static Persistent<String> oncomplete_sym;
static Persistent<String> onconnection_sym;
typedef class ReqWrap<uv_connect_t> ConnectWrap;
Local<Object> AddressToJS(const sockaddr* addr);
// add the function here.
// Below all the definitions at the top
Handle<Value> TCPWrap::Hello(const Arguments& args) {
}
Local<Object> TCPWrap::Instantiate() {
// If this assert fire then process.binding('tcp_wrap') hasn't been
// called yet.
assert(tcpConstructor.IsEmpty() == false);
HandleScope scope;
Local<Object> obj = tcpConstructor->NewInstance();
using v8::Value;
static Persistent<Function> tcpConstructor;
static Persistent<String> oncomplete_sym;
static Persistent<String> onconnection_sym;
typedef class ReqWrap<uv_connect_t> ConnectWrap;
Local<Object> AddressToJS(const sockaddr* addr);
// add the function here.
// Below all the definitions at the top
Handle<Value> TCPWrap::Hello(const Arguments& args) {
// Define a HandleScope. This lets the garbage collector
// do the magic thing with cleaning up any data we create
HandleScope scope;
}
Local<Object> TCPWrap::Instantiate() {
// If this assert fire then process.binding('tcp_wrap') hasn't been
// called yet.
assert(tcpConstructor.IsEmpty() == false);
HandleScope scope;
Local<Object> obj = tcpConstructor->NewInstance();
// add the function here.
// Below all the definitions at the top
Handle<Value> TCPWrap::Hello(const Arguments& args) {
// Define a HandleScope. This lets the garbage collector
// do the magic thing with cleaning up any data we create
HandleScope scope;
}
// add the function here.
// Below all the definitions at the top
Handle<Value> TCPWrap::Hello(const Arguments& args) {
// Define a HandleScope. This lets the garbage collector
// do the magic thing with cleaning up any data we create
HandleScope scope;
// Return the JavaScript string "world"
return scope.Close(String::New("world"));
}
void TCPWrap::Initialize(Handle<Object> target) {
// ...
// lots of TCP stuff
// ...
target->Set(String::NewSymbol("TCP"), tcpConstructor);
}
void TCPWrap::Initialize(Handle<Object> target) {
// ...
// lots of TCP stuff
// ...
target->Set(String::NewSymbol("TCP"), tcpConstructor);
// Export our function here.
NODE_SET_METHOD(target, "hello", Hello);
}
$ vim src/tcp_wrap.h
//
// blah blah copyright
//
#ifndef TCP_WRAP_H_
#define TCP_WRAP_H_
#include "stream_wrap.h"
namespace node {
class TCPWrap : public StreamWrap {
public:
// public stuff....
private:
// private stuff....
static v8::Handle<v8::Value> Connect6(const v8::Arguments& args);
static v8::Handle<v8::Value> Open(const v8::Arguments& args);
private:
// private stuff....
static v8::Handle<v8::Value> Connect6(const v8::Arguments& args);
static v8::Handle<v8::Value> Open(const v8::Arguments& args);
static v8::Handle<v8::Value> Hello(const v8::Arguments& args);
$ make -j4
make -C out BUILDTYPE=Release V=
CXX(target) /tmp/node/out/Release/obj.target/node/src/tcp_wrap.o
LINK(target) /tmp/node/out/Release/node
ld: warning: alignment lost in merging tentative definition _OPENSSL_ia32cap_P
ld: warning: _OPENSSL_ia32cap_P has different visibility (hidden) in
/tmp/node/out/Release/libopenssl.a(x86_64cpuid.o) and (default) in
/tmp/node/out/Release/libopenssl.a(cryptlib.o)
LINK(target) /tmp/node/out/Release/node: Finished
ln -fs out/Release/node node
## then demo the first part...
$ vim lib/net.js
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var events = require('events');
var stream = require('stream');
var timers = require('timers');
var util = require('util');
var assert = require('assert');
var cares = process.binding('cares_wrap');
var cluster;
function noop() {}
// ... lots more net.js stuff
function noop() {}
// Export the hello function
exports.hello = function() {
return process.binding('tcp_wrap').hello();
};
// ... lots more net.js stuff
$ ./node test/simple/test-net-hello.js
assert.js:92
throw new assert.AssertionError({
^
AssertionError: "undefined" == "function"
at Object.<anonymous> (/tmp/node/test/simple/test-net-hello.js:29:8)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:349:32)
at Function.Module._load (module.js:305:12)
at Function.Module.runMain (module.js:490:10)
at startup (node.js:119:16)
at node.js:827:3
$ make -j4
make -C out BUILDTYPE=Release V=
ACTION _tmp_node_node_gyp_node_js2c_host_node_js2c /tmp/node/out/Release/obj/gen/node_natives.h
CXX(target) /tmp/node/out/Release/obj.target/node/src/node_javascript.o
LINK(target) /tmp/node/out/Release/node
ld: warning: alignment lost in merging tentative definition _OPENSSL_ia32cap_P
ld: warning: _OPENSSL_ia32cap_P has different visibility (hidden) in
/tmp/node/out/Release/libopenssl.a(x86_64cpuid.o) and (default) in
/tmp/node/out/Release/libopenssl.a(cryptlib.o)
LINK(target) /tmp/node/out/Release/node: Finished
ln -fs out/Release/node node
$ ./node test/simple/test-net-hello.js
ok
## TEST PASSES! WoOo!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment