Moving this to nodejs, nodejs-dev is for core node development. On Tue, Jul 10, 2012 at 3:52 PM, aludin <[email protected]> wrote: > Hi, > > I am trying to return "wrapped objects" from a single object playing the > role an object factory. I ve tried something like this but it does nt seem > to work: > > // the ObjectWrap factory which also inherits from ObjectWrap. > > > class NoomraEngine: public node::ObjectWrap { > > > public: > > static Persistent<FunctionTemplate> s_ct; > > static void Initialize(Handle<Object> target); > > static Handle<Value> New(const Arguments& args); > > static Handle<Value> CreateInstrument(const Arguments& args); // > factory method for returning wrapped objects. > > > > NoomraEngine(); > > virtual ~NoomraEngine(); > > }; > > > > > Handle<Value> NoomraEngine::CreateInstrument(const Arguments& args) { > > HandleScope scope; > > // determine which object to create according to args. > > // so for example if it's a VanillaEquityOptionWrapper: > > return scope.Close(VanillaEquityOptionWrapper::New(args)); > > } > > > For example VanillaEquityOptionWrapper is declared as follows and should one > of the possible wrapped objects returned by NoomraEngine: > > class VanillaEquityOptionWrapper : public node::ObjectWrap { > > private: > > //VanillaEquityOption* equityOption; > > public: > > static Persistent<FunctionTemplate> create; > > static void Initialize(Handle<Object> target); > > static Handle<Value> New(const Arguments& args); > > static Handle<Value> Price(const Arguments& args); // mapped to > priceEquity for node. > > > > VanillaEquityOptionWrapper(); > > virtual ~VanillaEquityOptionWrapper(); > > }; > > > } /* namespace noomra */ > > #endif /* VANILLAEQUITYOPTIONWRAPPER_H_ */ > > > Here's the node output: > >> var addon = require('noomra'); > undefined >> var engine = new addon.NoomraEngine(); > undefined >> var equity = engine.createInstrument(); > undefined >> equity.priceEquity(); > TypeError: Object #<NoomraEngine> has no method 'priceEquity' > at repl:1:8 > at REPLServer.self.eval (repl.js:111:21) > at rli.on.e (repl.js:260:20) > at REPLServer.self.eval (repl.js:118:5) > at Interface.<anonymous> (repl.js:250:12) > at Interface.EventEmitter.emit (events.js:88:17) > at Interface._onLine (readline.js:183:10) > at Interface._line (readline.js:502:8) > at Interface._ttyWrite (readline.js:720:14) > at ReadStream.<anonymous> (readline.js:105:12)
The problem - if I read your code right - is that VanillaEquityOptionWrapper::New(args) is not a constructor call. You should probably call create->GetFunction()->NewInstance(argc, argv) instead but note that NewInstance() takes a Handle<Value> array, not an Arguments object. I don't know what problem you're trying to solve but consider moving more of your app logic from C++ to JS. You can set up constructors, factories, inheritance chains and what have you from C++ bindings but it's a) more work and b) tricky to get right. Doing it from inside a JS shim will make your life infinitely more pleasant. -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
