1 /*
2    Copyright (c) 2013, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "node.h"
26 
27 using namespace v8;
28 
29 /*
30 HandleScope scope;
31 
32  * A FunctionTemplate can have properties, these properties are added to the
33  * function object when it is created.
34 
35  * A FunctionTemplate has a corresponding instance template which is
36  * used to create object instances when the function is used as a
37  * constructor. Properties added to the instance template are added to
38  * each object instance.
39 
40     Local<FunctionTemplate> t = FunctionTemplate::New();
41     t->Set("func_property", Number::New(1));
42 
43     Local<v8::ObjectTemplate> proto_t = t->PrototypeTemplate();
44     //proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
45     proto_t->Set("proto_const", v8::Number::New(2));
46 
47     v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
48     //instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
49     //instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
50     instance_t->Set("instance_property", Number::New(3));
51 
52     v8::Local<v8::Function> function = t->GetFunction();
53     v8::Local<v8::Object> instance = function->NewInstance();
54 
55     target->Set(String::NewSymbol("jscons"), t);
56 */
57 
58 
pGetter(Local<String> property,const AccessorInfo & info)59 Handle<Value> pGetter(Local<String> property,
60                      const AccessorInfo & info)
61 {
62   /* info has Data(), This(), and Holder() */
63   HandleScope scope;
64   return scope.Close(info.Data());
65 }
66 
pSetter(Local<String> property,Local<Value> value,const AccessorInfo & info)67 void pSetter(Local<String> property,
68              Local<Value> value,
69              const AccessorInfo& info)
70 {
71 
72 }
73 
74 
75 
76 /*
77   var builder=require("./jscons").builder
78 
79    calling builder("foo", 66)
80      should return a constructor
81      for objects
82      with a property named "foo"
83      whose value is 66
84 */
theConstructor(const Arguments & args)85 Handle<Value> theConstructor(const Arguments &args) {
86   HandleScope scope;
87 
88   if(args.IsConstructCall()) {
89   }
90   else {
91     ThrowException(Exception::Error(String::New("must be a called as constructor")));
92   }
93   return args.This();
94 }
95 
96 
ConstructorBuilder(const Arguments & args)97 Handle<Value> ConstructorBuilder(const Arguments &args) {
98   HandleScope scope;
99   Local<String> methodName = args[0]->ToString();
100   Local<Value> magic = args[1];
101   Local<FunctionTemplate> ft = FunctionTemplate::New();
102 
103   ft->SetCallHandler(theConstructor);
104   ft->InstanceTemplate()->SetAccessor(methodName, pGetter, pSetter, magic);
105   ft->InstanceTemplate()->Set(String::NewSymbol("isMappedNdbRecord"),
106                               True(), DontEnum);
107   return scope.Close(ft->GetFunction());
108 }
109 
110 
initOnLoad(Handle<Object> target)111 void initOnLoad(Handle<Object> target) {
112   HandleScope scope;
113   Local<FunctionTemplate> ft = FunctionTemplate::New();
114   ft->SetCallHandler(ConstructorBuilder);
115 
116   target->Set(String::NewSymbol("builder"), ft->GetFunction());
117 }
118 
119 
120 /*  FINAL STEP.
121     This macro associates the module name with its initializer function
122 */
123 NODE_MODULE(jscons, initOnLoad)
124