[r9301]: main / trunk / interpreter / classes / PackageClass.cpp  Maximize  Restore  History

Download this file

650 lines (568 with data), 19.1 kB

  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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*----------------------------------------------------------------------------*/
/* */
/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
/* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the Common Public License v1.0 which accompanies this */
/* distribution. A copy is also available at the following address: */
/* http://www.oorexx.org/license.html */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the distribution. */
/* */
/* Neither the name of Rexx Language Association nor the names */
/* of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/*----------------------------------------------------------------------------*/
/******************************************************************************/
/* REXX Kernel PackageClass.cpp */
/* */
/* Primitive Package class */
/* */
/******************************************************************************/
#include "RexxCore.h"
#include "ArrayClass.hpp"
#include "SourceFile.hpp"
#include "DirectoryClass.hpp"
#include "ProtectedObject.hpp"
#include "PackageClass.hpp"
#include "RoutineClass.hpp"
#include "InterpreterInstance.hpp"
#include "PackageManager.hpp"
// singleton class instance
RexxClass *PackageClass::classInstance = OREF_NULL;
/**
* Create initial class object at bootstrap time.
*/
void PackageClass::createInstance()
{
CLASS_CREATE(Package, "Package", RexxClass);
}
void *PackageClass::operator new (size_t size)
/******************************************************************************/
/* Function: create a new method instance */
/******************************************************************************/
{
/* get a new method object */
return new_object(size, T_Package);
}
PackageClass::PackageClass(RexxSource *s)
/******************************************************************************/
/* Function: Initialize a method object */
/******************************************************************************/
{
OrefSet(this, this->source, s); /* store the code */
}
void PackageClass::live(size_t liveMark)
/******************************************************************************/
/* Function: Normal garbage collection live marking */
/******************************************************************************/
{
memory_mark(this->source);
memory_mark(this->objectVariables);
}
void PackageClass::liveGeneral(int reason)
/******************************************************************************/
/* Function: Generalized object marking */
/******************************************************************************/
{
memory_mark_general(this->source);
memory_mark_general(this->objectVariables);
}
void PackageClass::flatten(RexxEnvelope *envelope)
/******************************************************************************/
/* Function: Flatten an object */
/******************************************************************************/
{
setUpFlatten(PackageClass)
flatten_reference(newThis->source, envelope);
flatten_reference(newThis->objectVariables, envelope);
cleanUpFlatten
}
/**
* Get the program name of the package
*
* @return The name of the original source package.
*/
RexxString *PackageClass::getName()
{
return source->getProgramName();
}
/**
* Return all of the source lines for the package, as an array.
*
* @return The entire array of source lines.
*/
RexxArray *PackageClass::getSource()
{
return source->extractSource();
}
/**
* Extract a specific line from the program source.
*
* @param n The line position.
*
* @return The extracted line.
*/
RexxString *PackageClass::getSourceLine(size_t n)
{
return source->get(n);
}
/**
* Extract a specific line from the program source.
*
* @param n The line position.
*
* @return The extracted line.
*/
RexxString *PackageClass::getSourceLineRexx(RexxObject *position)
{
// the starting position isn't optional
size_t n = positionArgument(position, ARG_ONE);
return source->get(n);
}
/**
* Get the number of source lines in the package
*
* @return the count of lines
*/
RexxInteger *PackageClass::getSourceSize()
{
return new_integer(source->sourceSize());
}
/**
* Retrieve all classes defined by this package.
*
* @return A directory of all of the classes defined by this package.
*/
RexxDirectory *PackageClass::getClasses()
{
// we need to return a copy. The source might necessarily have any of these,
// so we return an empty directory if it's not there.
RexxDirectory *classes = source->getInstalledClasses();
if (classes != OREF_NULL)
{
return (RexxDirectory *)classes->copy();
}
else
{
return new_directory();
}
}
/**
* Retrieve all public classes defined by this package.
*
* @return A directory of the public classes.
*/
RexxDirectory *PackageClass::getPublicClasses()
{
// we need to return a copy. The source might necessarily have any of these,
// so we return an empty directory if it's not there.
RexxDirectory *classes = source->getInstalledPublicClasses();
if (classes != OREF_NULL)
{
return (RexxDirectory *)classes->copy();
}
else
{
return new_directory();
}
}
/**
* Retrieve all of the classes imported into this package from
* other packages.
*
* @return A directory of the imported classes.
*/
RexxDirectory *PackageClass::getImportedClasses()
{
// we need to return a copy. The source might necessarily have any of these,
// so we return an empty directory if it's not there.
RexxDirectory *classes = source->getImportedClasses();
if (classes != OREF_NULL)
{
return (RexxDirectory *)classes->copy();
}
else
{
return new_directory();
}
}
/**
* Get a list of all routines defined by this package.
*
* @return A directory of the routines.
*/
RexxDirectory *PackageClass::getRoutines()
{
// we need to return a copy. The source might necessarily have any of these,
// so we return an empty directory if it's not there.
RexxDirectory *routines = source->getInstalledRoutines();
if (routines != OREF_NULL)
{
return (RexxDirectory *)routines->copy();
}
else
{
return new_directory();
}
}
/**
* Return a directory of the Public routines defined by this
* package.
*
* @return A directory holding the public routines.
*/
RexxDirectory *PackageClass::getPublicRoutines()
{
// we need to return a copy. The source might necessarily have any of these,
// so we return an empty directory if it's not there.
RexxDirectory *routines = source->getInstalledPublicRoutines();
if (routines != OREF_NULL)
{
return (RexxDirectory *)routines->copy();
}
else
{
return new_directory();
}
}
/**
* Get the directory of routines that have been imported into
* to this package form other packages.
*
* @return A directory of the imported routines.
*/
RexxDirectory *PackageClass::getImportedRoutines()
{
// we need to return a copy. The source might necessarily have any of these,
// so we return an empty directory if it's not there.
RexxDirectory *routines = source->getImportedRoutines();
if (routines != OREF_NULL)
{
return (RexxDirectory *)routines->copy();
}
else
{
return new_directory();
}
}
/**
* Get all of the unattached methods defined in this package.
*
* @return A directory of the unattached methods.
*/
RexxDirectory *PackageClass::getMethods()
{
// we need to return a copy. The source might necessarily have any of these,
// so we return an empty directory if it's not there.
RexxDirectory *methods = source->getMethods();
if (methods != OREF_NULL)
{
return (RexxDirectory *)methods->copy();
}
else
{
return new_directory();
}
}
/**
* Get all of the packages that have been added to this package
* context.
*
* @return An array of the added packages.
*/
RexxArray *PackageClass::getImportedPackages()
{
RexxList *packages = source->getPackages();
if (packages != OREF_NULL)
{
return packages->makeArray();
}
else
{
return new_array((size_t)0);
}
}
/**
* Load a package in a source context.
*
* @param name The target package name.
* @param s The optional source lines for the package, as an array.
*
* @return The loaded package object.
*/
PackageClass *PackageClass::loadPackage(RexxString *name, RexxArray *s)
{
// make sure we have a valid name and delegate to the source object
name = stringArgument(name, 1);
// if no source provided, this comes from a file
if (s == OREF_NULL)
{
return source->loadRequires(ActivityManager::currentActivity, name);
}
else
{
s = arrayArgument(s, "source");
return source->loadRequires(ActivityManager::currentActivity, name, s);
}
}
/**
* Load a package in a source context.
*
* @param name The target package name.
*
* @return The loaded package object.
*/
RexxObject *PackageClass::addPackage(PackageClass *package)
{
classArgument(package, ThePackageClass, "package");
source->addPackage(package);
return this;
}
/**
* Add a routine to this package's private routine list.
*
* @param routine The routine to add.
*
* @return The target package object.
*/
RexxObject *PackageClass::addRoutine(RexxString *name, RoutineClass *routine)
{
name = stringArgument(name, "name");
classArgument(routine, TheRoutineClass, "routine");
source->addInstalledRoutine(name, routine, false);
return this;
}
/**
* Add a routine to this package's public routine list.
*
* @param routine The routine to add.
*
* @return The target package object.
*/
RexxObject *PackageClass::addPublicRoutine(RexxString *name, RoutineClass *routine)
{
name = stringArgument(name, "name");
classArgument(routine, TheRoutineClass, "routine");
source->addInstalledRoutine(name, routine, true);
return this;
}
/**
* Add a class to this package's class list.
*
* @param clazz The class to add.
*
* @return The target package object.
*/
RexxObject *PackageClass::addClass(RexxString *name, RexxClass *clazz)
{
name = stringArgument(name, "name");
classArgument(clazz, TheClassClass, "class");
source->addInstalledClass(name, clazz, false);
return this;
}
/**
* Add a class to this package's public class list.
*
* @param clazz The class to add.
*
* @return The target package object.
*/
RexxObject *PackageClass::addPublicClass(RexxString *name, RexxClass *clazz)
{
name = stringArgument(name, "name");
classArgument(clazz, TheClassClass, "class");
source->addInstalledClass(name, clazz, true);
return this;
}
/**
* Resolve a class in the context of a package.
*
* @param name The required class name.
*
* @return The resolved class object.
*/
RexxClass *PackageClass::findClass(RexxString *name)
{
RexxClass *classObj = source->findClass(name);
// we need to filter this to always return a class object
if (classObj != OREF_NULL && classObj->isInstanceOf(TheClassClass))
{
return classObj;
}
return OREF_NULL;
}
/**
* Resolve a class in the context of a package.
*
* @param name The required class name.
*
* @return The resolved class object.
*/
RexxClass *PackageClass::findClassRexx(RexxString *name)
{
name = stringArgument(name, "name");
RexxClass *cls = source->findClass(name);
if (cls == OREF_NULL)
{
return (RexxClass *)TheNilObject;
}
return cls;
}
/**
* Resolve a routine in the context of a package.
*
* @param name The required routine name.
*
* @return The resolved routine object.
*/
RoutineClass *PackageClass::findRoutine(RexxString *name)
{
return source->findRoutine(name);
}
/**
* Resolve a routine in the context of a package.
*
* @param name The required routine name.
*
* @return The resolved routine object.
*/
RoutineClass *PackageClass::findRoutineRexx(RexxString *name)
{
name = stringArgument(name, "name");
RoutineClass *routine = findRoutine(name);
if (routine == OREF_NULL)
{
return (RoutineClass *)TheNilObject;
}
return routine;
}
/**
* Set a security manager on a package.
*
* @param manager The security manager object.
*
* @return The security manager object.
*/
RexxObject *PackageClass::setSecurityManager(RexxObject *manager)
{
source->setSecurityManager(manager);
return TheTrueObject;
}
PackageClass *PackageClass::newRexx(
RexxObject **init_args, /* subclass init arguments */
size_t argCount) /* number of arguments passed */
/******************************************************************************/
/* Function: Create a new packag from REXX code contained in a file or an */
/* array */
/******************************************************************************/
{
RexxObject *pgmname; /* method name */
RexxObject *_source; /* Array or string object */
size_t initCount = 0; /* count of arguments we pass along */
RexxActivity *activity = ActivityManager::currentActivity;
InterpreterInstance *instance = activity->getInstance();
/* break up the arguments */
RexxClass::processNewArgs(init_args, argCount, &init_args, &initCount, 2, (RexxObject **)&pgmname, (RexxObject **)&_source);
PackageClass *package = OREF_NULL;
/* get the package name as a string */
RexxString *nameString = stringArgument(pgmname, "name");
if (_source == OREF_NULL)
{
// if no directly provided source, resolve the name in the global context and have the instance
// load the file.
RexxString *resolvedName = instance->resolveProgramName(nameString, OREF_NULL, OREF_NULL);
ProtectedObject n(resolvedName);
package = instance->loadRequires(activity, nameString, resolvedName);
}
else
{
// add this to the instance context
RexxArray *sourceArray = arrayArgument(_source, "source");
package = instance->loadRequires(activity, nameString, sourceArray);
}
ProtectedObject p(package);
/* Give new object its behaviour */
package->setBehaviour(((RexxClass *)this)->getInstanceBehaviour());
if (((RexxClass *)this)->hasUninitDefined())
{
package->hasUninit(); /* Make sure everyone is notified. */
}
/* now send an INIT message */
package->sendMessage(OREF_INIT, init_args, initCount);
return package; /* return the new method */
}
/**
* Dynamically load a library package
*
* @param name The required package name.
*
* @return True if the package was loaded and resolved, false if
* the package could not be loaded.
*/
RexxObject *PackageClass::loadLibrary(RexxString *name)
{
name = stringArgument(name, "name");
// have we already loaded this package?
// may need to bootstrap it up first.
LibraryPackage *package = PackageManager::loadLibrary(name);
return package == NULL ? TheFalseObject : TheTrueObject;
}
/**
* Return the package-defined digits setting
*
* @return The digits setting defined for this package.
*/
RexxObject *PackageClass::digits()
{
return new_integer(source->getDigits());
}
/**
* Return the package-defined default fuzz setting.
*
* @return The package defined fuzz setting.
*/
RexxObject *PackageClass::fuzz()
{
return new_integer(source->getFuzz());
}
/**
* Return the package-defined default form setting.
*
* @return The default form setting.
*/
RexxObject *PackageClass::form()
{
return source->getForm() == Numerics::FORM_SCIENTIFIC ? OREF_SCIENTIFIC : OREF_ENGINEERING;
}
/**
* Return the package-defined default trace setting.
*
* @return The string-formatted trace setting.
*/
RexxObject *PackageClass::trace()
{
return source->getTrace();
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks