1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/*
* @file ejsObject.c
* @brief Object class
*/
/********************************* Copyright **********************************/
/*
* @copy default
*
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
*
* This software is distributed under commercial and open source licenses.
* You may use the GPL open source license described below or you may acquire
* a commercial license from Mbedthis Software. You agree to be fully bound
* by the terms of either license. Consult the LICENSE.TXT distributed with
* this software for full details.
*
* This software is open source; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See the GNU General Public License for more
* details at: http://www.mbedthis.com/downloads/gplLicense.html
*
* This program is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This GPL license does NOT permit incorporating this software into
* proprietary programs. If you are unable to comply with the GPL, you must
* acquire a commercial license to use this software. Commercial licenses
* for this software and support services are available from Mbedthis
* Software at http://www.mbedthis.com
*
* @end
*/
/********************************** Includes **********************************/
#include "ejs.h"
#if BLD_FEATURE_EJS
/****************************** Forward Declarations **************************/
/*
* Support routines
*/
static void formatVar(Ejs *ep, MprBuf *bp, EjsVar *vp);
/******************************************************************************/
/*
* Routine to create an object of the desired class. Class name may
* contain "."
*
* The created object will be a stand-alone class NOT entered into the
* properties of any other object. Callers must do this if required. ClassName
* may contain "." and is interpreted relative to "obj" if supplied.
*
* Note: this does not call the constructors for the various objects and base
* classes.
*/
EjsVar *ejsCreateSimpleObjInternal(EJS_LOC_DEC(ep, loc), const char *className)
{
EjsVar *baseClass;
if (className && *className) {
baseClass = ejsGetClass(ep, 0, className);
if (baseClass == 0) {
mprError(ep, MPR_LOC, "Can't find base class %s", className);
return 0;
}
} else {
baseClass = 0;
}
return ejsCreateSimpleObjUsingClassInt(EJS_LOC_PASS(ep, loc),
baseClass);
}
/******************************************************************************/
/*
* Create an object based upon the specified base class object. It will be a
* stand-alone class not entered into the properties of any other object.
* Callers must do this if required.
*
* Note: this does not call the constructors for the various objects and base
* classes.
*/
EjsVar *ejsCreateSimpleObjUsingClassInt(EJS_LOC_DEC(ep, loc),
EjsVar *baseClass)
{
EjsVar *vp;
mprAssert(baseClass);
if (baseClass == 0) {
mprError(ep, MPR_LOC, "Missing base class\n");
return 0;
}
vp = ejsCreateObjVarInternal(EJS_LOC_PASS(ep, loc));
if (vp == 0) {
return vp;
}
ejsSetBaseClass(vp, baseClass);
/*
* This makes all internal method accesses faster
* NOTE: this code is duplicated in ejsCreateSimpleClass
*/
mprAssert(vp->objectState);
vp->objectState->methods = baseClass->objectState->methods;
return vp;
}
/******************************************************************************/
void ejsSetMethods(Ejs *ep, EjsVar *op)
{
op->objectState->methods = ep->global->objectState->methods;
}
/******************************************************************************/
/******************************** Internal Methods ****************************/
/******************************************************************************/
static EjsVar *createObjProperty(Ejs *ep, EjsVar *obj, const char *property)
{
return ejsGetVarPtr(ejsCreateSimpleProperty(ep, obj, property));
}
/******************************************************************************/
static int deleteObjProperty(Ejs *ep, EjsVar *obj, const char *property)
{
return ejsDeleteProperty(ep, obj, property);
}
/******************************************************************************/
static EjsVar *getObjProperty(Ejs *ep, EjsVar *obj, const char *property)
{
return ejsGetVarPtr(ejsGetSimpleProperty(ep, obj, property));
}
/******************************************************************************/
/*
* Set the value of a property. Create if it does not exist
*/
static EjsVar *setObjProperty(Ejs *ep, EjsVar *obj, const char *property,
const EjsVar *value)
{
EjsProperty *pp;
EjsVar *vp;
pp = ejsCreateSimpleProperty(ep, obj, property);
if (pp == 0) {
mprAssert(pp);
return 0;
}
vp = ejsGetVarPtr(pp);
if (ejsWriteVar(ep, vp, value, EJS_SHALLOW_COPY) < 0) {
mprAssert(0);
return 0;
}
return ejsGetVarPtr(pp);
}
/******************************************************************************/
/*********************************** Constructors *****************************/
/******************************************************************************/
#if UNUSED
/*
* Object constructor. We don't use this for speed. Think very carefully if
* you add an object constructor.
*/
int ejsObjectConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
return 0;
}
#endif
/******************************************************************************/
/******************************** Visible Methods *****************************/
/******************************************************************************/
static int cloneMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
int copyDepth;
copyDepth = EJS_DEEP_COPY;
if (argc == 1 && ejsVarToBoolean(argv[0])) {
copyDepth = EJS_RECURSIVE_DEEP_COPY;
}
ejsWriteVar(ep, ep->result, thisObj, copyDepth);
return 0;
}
/******************************************************************************/
static int toStringMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
MprBuf *bp;
int saveMaxDepth, saveDepth, saveFlags;
saveMaxDepth = ep->maxDepth;
if (argc >= 1) {
ep->maxDepth = ejsVarToInteger(argv[0]);
} else if (ep->maxDepth == 0) {
ep->maxDepth = MAXINT;
}
saveFlags = ep->flags;
if (argc >= 2) {
if (ejsVarToBoolean(argv[1])) {
ep->flags |= EJS_FLAGS_ENUM_HIDDEN;
}
}
if (argc == 3) {
if (ejsVarToBoolean(argv[2])) {
ep->flags |= EJS_FLAGS_ENUM_BASE;
}
}
bp = mprCreateBuf(ep, 0, 0);
saveDepth = ep->depth;
formatVar(ep, bp, thisObj);
ep->depth = saveDepth;
ep->maxDepth = saveMaxDepth;
mprAddNullToBuf(bp);
ejsWriteVarAsString(ep, ep->result, mprGetBufStart(bp));
mprFree(bp);
ep->flags = saveFlags;
return 0;
}
/******************************************************************************/
static int valueOfMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
if (argc != 0) {
mprAssert(0);
return -1;
}
switch (thisObj->type) {
default:
case EJS_TYPE_UNDEFINED:
case EJS_TYPE_NULL:
case EJS_TYPE_CMETHOD:
case EJS_TYPE_OBJECT:
case EJS_TYPE_METHOD:
case EJS_TYPE_STRING_CMETHOD:
ejsWriteVar(ep, ep->result, thisObj, EJS_SHALLOW_COPY);
break;
case EJS_TYPE_STRING:
ejsWriteVarAsInteger(ep, ep->result, atoi(thisObj->string));
break;
case EJS_TYPE_BOOL:
case EJS_TYPE_INT:
#if BLD_FEATURE_INT64
case EJS_TYPE_INT64:
#endif
#if BLD_FEATURE_FLOATING_POINT
case EJS_TYPE_FLOAT:
#endif
ejsWriteVar(ep, ep->result, thisObj, EJS_SHALLOW_COPY);
break;
}
return 0;
}
/******************************************************************************/
static int hashGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
ejsSetReturnValueToInteger(ejs, (int) thisObj->objectState);
return 0;
}
/******************************************************************************/
static int classGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
if (thisObj->objectState == 0 || thisObj->objectState->baseClass == 0) {
ejsSetReturnValueToString(ejs, "object");
} else {
ejsSetReturnValueToString(ejs,
thisObj->objectState->baseClass->objectState->className);
}
return 0;
}
/******************************************************************************/
/*
* Format an object. Called recursively to format properties and contained
* objects.
*/
static void formatVar(Ejs *ep, MprBuf *bp, EjsVar *vp)
{
EjsProperty *pp, *first;
EjsVar *propVar, *baseClass;
char *buf, *value;
int i;
if (vp->type == EJS_TYPE_OBJECT) {
if (!vp->objectState->visited) {
mprPutStringToBuf(bp, vp->isArray ? "[\n" : "{\n");
ep->depth++;
vp->objectState->visited = 1;
if (ep->depth <= ep->maxDepth) {
first = ejsGetFirstProperty(vp, EJS_ENUM_ALL);
if (ep->flags & EJS_FLAGS_ENUM_BASE) {
baseClass = vp->objectState->baseClass;
if (baseClass) {
for (i = 0; i < ep->depth; i++) {
mprPutStringToBuf(bp, " ");
}
mprPutStringToBuf(bp, baseClass->objectState->objName);
mprPutStringToBuf(bp, ": /* Base Class */ ");
if (baseClass->objectState == vp->objectState) {
value = "this";
} else if (ejsRunMethodCmd(ep, baseClass, "toString",
"%d", ep->maxDepth) < 0) {
value = "[object Object]";
} else {
mprAssert(ejsVarIsString(ep->result));
value = ep->result->string;
}
mprPutStringToBuf(bp, value);
if (first) {
mprPutStringToBuf(bp, ",\n");
}
}
}
pp = first;
while (pp) {
if (! pp->dontEnumerate ||
ep->flags & EJS_FLAGS_ENUM_HIDDEN) {
for (i = 0; i < ep->depth; i++) {
mprPutStringToBuf(bp, " ");
}
if (! vp->isArray) {
mprPutStringToBuf(bp, pp->name);
mprPutStringToBuf(bp, ": ");
}
propVar = ejsGetVarPtr(pp);
if (propVar->type == EJS_TYPE_OBJECT) {
if (pp->var.objectState == vp->objectState) {
value = "this";
} else if (ejsRunMethodCmd(ep, propVar,
"toString", "%d", ep->maxDepth) < 0) {
value = "[object Object]";
} else {
mprAssert(ejsVarIsString(ep->result));
value = ep->result->string;
}
mprPutStringToBuf(bp, value);
} else {
formatVar(ep, bp, &pp->var);
}
pp = ejsGetNextProperty(pp, EJS_ENUM_ALL);
if (pp) {
mprPutStringToBuf(bp, ",\n");
}
} else {
pp = ejsGetNextProperty(pp, EJS_ENUM_ALL);
}
}
}
vp->objectState->visited = 0;
mprPutCharToBuf(bp, '\n');
ep->depth--;
for (i = 0; i < ep->depth; i++) {
mprPutStringToBuf(bp, " ");
}
mprPutCharToBuf(bp, vp->isArray ? ']' : '}');
}
} else if (vp->type == EJS_TYPE_METHOD) {
mprPutStringToBuf(bp, "function (");
for (i = 0; i < vp->method.args->length; i++) {
mprPutStringToBuf(bp, vp->method.args->items[i]);
if ((i + 1) < vp->method.args->length) {
mprPutStringToBuf(bp, ", ");
}
}
mprPutStringToBuf(bp, ") {");
mprPutStringToBuf(bp, vp->method.body);
for (i = 0; i < ep->depth; i++) {
mprPutStringToBuf(bp, " ");
}
mprPutStringToBuf(bp, "}");
} else {
if (vp->type == EJS_TYPE_STRING) {
mprPutCharToBuf(bp, '\"');
}
/*
* We don't use ejsVarToString for arrays, objects and strings.
* This is because ejsVarToString does not call "obj.toString"
* and it is not required for strings.
* MOB - rc
*/
buf = ejsVarToString(ep, vp);
mprPutStringToBuf(bp, buf);
if (vp->type == EJS_TYPE_STRING) {
mprPutCharToBuf(bp, '\"');
}
}
}
/******************************************************************************/
/*
* mixin code. Blends code at the "thisObj" level.
*/
static int mixinMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
EjsProperty *pp;
char *buf;
int fid, i, rc;
mprAssert(argv);
/*
* Create a variable scope block set to the current object
*/
rc = 0;
fid = ejsSetBlock(ep, thisObj);
for (i = 0; i < argc; i++) {
if (ejsVarIsString(argv[i])) {
rc = ejsEvalScript(ep, argv[i]->string, 0);
} else if (ejsVarIsObject(argv[i])) {
/* MOB -- OPT. When we have proper scope chains, we should just
refer to the module and not copy */
pp = ejsGetFirstProperty(argv[i], EJS_ENUM_ALL);
while (pp) {
ejsSetProperty(ep, thisObj, pp->name, ejsGetVarPtr(pp));
pp = ejsGetNextProperty(pp, EJS_ENUM_ALL);
}
} else {
/* MOB - rc */
buf = ejsVarToString(ep, argv[i]);
rc = ejsEvalScript(ep, buf, 0);
}
if (rc < 0) {
ejsCloseBlock(ep, fid);
return -1;
}
}
ejsCloseBlock(ep, fid);
return 0;
}
/******************************************************************************/
/*
* Create the object class
*/
int ejsDefineObjectClass(Ejs *ep)
{
EjsMethods *methods;
EjsProperty *objectProp, *protoProp;
EjsVar *op, *globalClass;
/*
* Must specially hand-craft the object class as it is the base class
* of all objects.
*/
op = ejsCreateObjVar(ep);
if (op == 0) {
return MPR_ERR_CANT_CREATE;
}
ejsSetClassName(ep, op, "Object");
/*
* Don't use a constructor for objects for speed
*/
ejsMakeClassNoConstructor(op);
/*
* MOB -- should mark properties as public / private and class or instance.
*/
ejsDefineCMethod(ep, op, "clone", cloneMethod, EJS_NO_LOCAL);
ejsDefineCMethod(ep, op, "toString", toStringMethod, EJS_NO_LOCAL);
ejsDefineCMethod(ep, op, "valueOf", valueOfMethod, EJS_NO_LOCAL);
ejsDefineCMethod(ep, op, "mixin", mixinMethod, EJS_NO_LOCAL);
ejsDefineCAccessors(ep, op, "hash", hashGetAccessor, 0, EJS_NO_LOCAL);
ejsDefineCAccessors(ep, op, "baseClass", classGetAccessor, 0, EJS_NO_LOCAL);
/*
* MOB -- make this an accessor
*/
protoProp = ejsSetProperty(ep, op, "prototype", op);
if (protoProp == 0) {
ejsFreeVar(ep, op);
return MPR_ERR_CANT_CREATE;
}
/*
* Setup the internal methods. Most classes will never override these.
* The XML class will. We rely on talloc to free internal. Use "ep" as
* the parent as we need "methods" to live while the interpreter lives.
*/
methods = mprAllocTypeZeroed(ep, EjsMethods);
op->objectState->methods = methods;
methods->createProperty = createObjProperty;
methods->deleteProperty = deleteObjProperty;
methods->getProperty = getObjProperty;
methods->setProperty = setObjProperty;
objectProp = ejsSetPropertyAndFree(ep, ep->global, "Object", op);
/*
* Change the global class to use Object's methods
*/
globalClass = ep->service->globalClass;
globalClass->objectState->methods = methods;
globalClass->objectState->baseClass = ejsGetVarPtr(protoProp);
ep->objectClass = ejsGetVarPtr(objectProp);
if (ejsObjHasErrors(ejsGetVarPtr(objectProp))) {
ejsFreeVar(ep, op);
return MPR_ERR_CANT_CREATE;
}
return 0;
}
/******************************************************************************/
#else
void ejsObjectDummy() {}
/******************************************************************************/
#endif /* BLD_FEATURE_EJS */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim:tw=78
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|