ooRexx  4.2.0-source
About: ooRexx (Open Object Rexx) is a free implementation of Object Rexx. Object Rexx is an enhancement of the classic Rexx interpreter; a full-featured programming language with a human-oriented syntax.
  Fossies Dox: ooRexx-4.2.0-source.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

LibraryPackage.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel */
40 /* */
41 /* Primitive LibraryPackage management */
42 /* */
43 /******************************************************************************/
44 
45 #include "RexxCore.h"
46 #include "LibraryPackage.hpp"
47 #include "PackageManager.hpp"
48 #include "Interpreter.hpp"
49 #include "RexxNativeCode.hpp"
50 #include "DirectoryClass.hpp"
51 #include "RoutineClass.hpp"
52 #include "ProtectedObject.hpp"
53 
61 void *LibraryPackage::operator new(size_t size)
62 {
63  return new_object(size, T_LibraryPackage);
64 }
65 
66 
74 {
75  OrefSet(this, libraryName, n);
76 }
77 
87 {
88  OrefSet(this, libraryName, n);
89  ProtectedObject p2(this);
90  // store the registered package entry
91  package = p;
92  // this is an internal package.
93  internal = true;
94 }
95 
99 void LibraryPackage::live(size_t liveMark)
100 {
104 }
105 
110 {
114 }
115 
116 
137 {
138  // try to load the package table.
139  package = getPackageTable();
140  // if this is NULL, return false to the manager
141  if (package == NULL)
142  {
143  return false;
144  }
145  // call the loader to get the package tables and set them up.
146  loadPackage();
147  return true;
148 }
149 
150 
151 
156 {
157  // call an unloader, if we have one.
158  if (package->unloader != NULL)
159  {
160  // go run the dispatcher call
162 
164  }
165  // the internal packages don't get unloaded because
166  // we'll be reusing the definition
167  if (loaded && !internal)
168  {
169  lib.unload();
170  }
171 }
172 
173 
182 {
183  // first try to load the libary
184  PACKAGE_LOADER loader;
185  // reset the library handle that was saved in the image.
186  lib.reset();
187 
189  {
190  // we don't report an exception here. This may have
191  // just been a probe attempt to see if we're real. We'll
192  // leave the exception decisions up to the package manager.
193  return NULL;
194  }
195 
196  // we're loaded now, vs. just a package fronting a name.
197  loaded = true;
198  // the try to resolve a package getting structure
199  // resolve the function address
200  void *entry = lib.getProcedure("RexxGetPackage");
201  if (entry == NULL)
202  {
203  // again, this is not an exception...this could just be
204  // a classic style function registration.
205  return NULL;
206  }
207  loader = (PACKAGE_LOADER)entry;
208  // call the loader to get the package tables and set them up.
209  return (*loader)();
210 }
211 
212 
219 {
220  // verify that this library is compatible
222  {
224  }
225  // load the function table
227 
228  // call a loader, if we have one.
229  if (package->loader != NULL)
230  {
231  // go run the dispatcher call
233 
235  }
236 }
237 
238 
246 {
247  // no routines exported by this package? Just return without
248  // doing anything.
249  if (table == NULL)
250  {
251  return;
252  }
253 
254  // create a directory of loaded routines
255  OrefSet(this, routines, new_directory());
256 
257  while (table->style != 0)
258  {
259  // table names tend to be specified in friendly form, we need to
260  // convert them to uppercase because "normal" Rexx function names
261  // tend to be uppercase.
262  RexxString *target = new_upper_string(table->name);
263  RexxString *routineName = new_string(table->name);
264 
265  RexxRoutine *func = OREF_NULL;
266  if (table->style == ROUTINE_CLASSIC_STYLE)
267  {
268  func = new RegisteredRoutine(libraryName, routineName, (RexxRoutineHandler *)table->entryPoint);
269  }
270  else
271  {
272  func = new RexxNativeRoutine(libraryName, routineName, (PNATIVEROUTINE)table->entryPoint);
273  }
274 
275  RoutineClass *routine = new RoutineClass(routineName, func);
276  // add this to our local table. Our local table needs to keep the original case,
277  // since those will be referenced by ::ROUTINE statements.
278  routines->put(routine, routineName);
279 
280  // add this to the global function pool
281  PackageManager::addPackageRoutine(target, routine);
282  // step to the next table entry
283  table++;
284  }
285 }
286 
287 
288 
299 {
300  RexxMethodEntry *entry = package->methods;
301 
302  if (entry != NULL)
303  {
304  // scan the exported method table for the required method
305  while (entry->style != 0)
306  {
307  // is this one a name match? Make a method, add it to
308  // the table, and return.
309  if (name->strCaselessCompare(entry->name))
310  {
311  return entry;
312  }
313  entry++;
314  }
315  }
316  return NULL;
317 }
318 
319 
330 {
332 
333  if ( entry != NULL )
334  {
335  // scan the exported method table for the required method
336  while (entry->style != 0)
337  {
338  // is this one a name match? Make a method, add it to
339  // the table, and return.
340  if (name->strCaselessCompare(entry->name))
341  {
342  return entry;
343  }
344  entry++;
345  }
346  }
347  return NULL;
348 }
349 
350 
360 {
361  // create our methods table if not yet created.
362  if (methods == OREF_NULL)
363  {
364  OrefSet(this, methods, new_directory());
365  }
366 
367  // see if this is in the table yet.
368  RexxNativeMethod *code = (RexxNativeMethod *)methods->at(name);
369  if (code == OREF_NULL)
370  {
371  // find the package definition
372  RexxMethodEntry *entry = locateMethodEntry(name);
373  // if we found one with this name, create a native method out of it.
374  if (entry != NULL)
375  {
376  code = new RexxNativeMethod(libraryName, name, (PNATIVEMETHOD)entry->entryPoint);
377  methods->put((RexxObject *)code, name);
378  return code;
379  }
380  // This, we know from nothing....
381  return OREF_NULL;
382  }
383  // had this cached already.
384  return code;
385 }
386 
387 
396 {
397  // we resolve all of these at load time, so this is either in the table, or it's not.
398  return (RoutineClass *)routines->at(name);
399 }
400 
401 
411 {
412  // find the package definition
413  RexxMethodEntry *entry = locateMethodEntry(name);
414  // if no entry, something bad has gone wrong
415  if (entry == NULL)
416  {
418  }
419  return (PNATIVEMETHOD)entry->entryPoint;
420 }
421 
422 
432 {
433  // find the package definition
434  RexxRoutineEntry *entry = locateRoutineEntry(name);
435  // if no entry, something bad has gone wrong
436  if (entry == NULL)
437  {
439  }
440 
441  // style mismatch...this is incompatible
442  if (entry->style == ROUTINE_CLASSIC_STYLE)
443  {
445  }
446  return (PNATIVEROUTINE)entry->entryPoint;
447 }
448 
449 
459 {
460  // find the package definition
461  RexxRoutineEntry *entry = locateRoutineEntry(name);
462  // if no entry, something bad has gone wrong
463  if (entry == NULL)
464  {
466  }
467 
468  // style mismatch...this is incompatible
469  if (entry->style != ROUTINE_CLASSIC_STYLE)
470  {
472  }
473  return (PREGISTEREDROUTINE)entry->entryPoint;
474 }
475 
476 
481 {
482  package = getPackageTable();
483  if (package == OREF_NULL)
484  {
485  Interpreter::logicError("Failure loading required base library");
486  }
487 }
488 
489 
496 {
497  package = pack;
498 }
499 
500 
501 
506 {
507  RexxThreadContext *context = activity->getThreadContext();
508 
509  loader(context);
510 }
511 
512 
513 
518 {
519  RexxThreadContext *context = activity->getThreadContext();
520 
521  unloader(context);
522 }
_RexxMethodEntry
Definition: oorexxapi.h:197
LibraryLoaderDispatcher
Definition: LibraryPackage.hpp:101
_RexxMethodEntry::name
const char * name
Definition: oorexxapi.h:201
RegisteredRoutine
Definition: RexxNativeCode.hpp:130
PNATIVEMETHOD
uint16_t *(RexxEntry * PNATIVEMETHOD)(RexxMethodContext *, ValueDescriptor *)
Definition: MethodClass.hpp:76
LibraryPackage::resolveRoutine
RoutineClass * resolveRoutine(RexxString *name)
Definition: LibraryPackage.cpp:395
_RexxRoutineEntry::style
int style
Definition: oorexxapi.h:178
_RexxPackageEntry::routines
struct _RexxRoutineEntry * routines
Definition: oorexxapi.h:250
LibraryLoaderDispatcher::run
virtual void run()
Definition: LibraryPackage.cpp:505
_RexxRoutineEntry
Definition: oorexxapi.h:176
T_LibraryPackage
Definition: ClassTypeCodes.h:113
LibraryPackage.hpp
RexxThreadContext_
Definition: oorexxapi.h:710
memory_mark_general
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:437
LibraryPackage::loaded
bool loaded
Definition: LibraryPackage.hpp:96
OrefSet
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
RoutineClass.hpp
RoutineClass
Definition: RoutineClass.hpp:49
PackageManager::addPackageRoutine
static void addPackageRoutine(RexxString *name, RoutineClass *func)
Definition: PackageManager.cpp:503
RexxActivity::run
void run()
Definition: RexxActivity.cpp:1384
RexxDirectory::at
RexxObject * at(RexxString *)
Definition: DirectoryClass.cpp:567
SysLibrary::unload
bool unload()
Definition: SysLibrary.cpp:111
reportException
void reportException(wholenumber_t error)
Definition: ActivityManager.hpp:136
Error_Execution_library_method
#define Error_Execution_library_method
Definition: RexxErrorCodes.h:555
RexxString::strCaselessCompare
bool strCaselessCompare(const char *s)
Definition: StringClass.hpp:347
RexxString::getStringData
const char * getStringData()
Definition: StringClass.hpp:333
PACKAGE_LOADER
RexxPackageEntry *RexxEntry * PACKAGE_LOADER()
Definition: LibraryPackage.hpp:55
LibraryPackage::resolveRoutineEntry
PNATIVEROUTINE resolveRoutineEntry(RexxString *name)
Definition: LibraryPackage.cpp:431
LibraryPackage::locateMethodEntry
RexxMethodEntry * locateMethodEntry(RexxString *name)
Definition: LibraryPackage.cpp:298
Interpreter.hpp
PNATIVEROUTINE
uint16_t *(RexxEntry * PNATIVEROUTINE)(RexxCallContext *, ValueDescriptor *)
Definition: MethodClass.hpp:78
LibraryPackage::loadPackage
void loadPackage()
Definition: LibraryPackage.cpp:218
RexxRoutineHandler
size_t REXXENTRY RexxRoutineHandler(CSTRING, size_t, PCONSTRXSTRING, CSTRING, PRXSTRING)
Definition: rexx.h:406
new_string
RexxString * new_string(const char *s, stringsize_t l)
Definition: StringClass.hpp:524
OREF_NULL
#define OREF_NULL
Definition: RexxCore.h:60
ROUTINE_CLASSIC_STYLE
#define ROUTINE_CLASSIC_STYLE
Definition: oorexxapi.h:187
LibraryPackage::unload
void unload()
Definition: LibraryPackage.cpp:155
_RexxPackageEntry::unloader
RexxPackageUnloader unloader
Definition: oorexxapi.h:249
ProtectedObject.hpp
ProtectedObject
Definition: ProtectedObject.hpp:46
SysLibrary::getProcedure
void * getProcedure(const char *name)
Definition: SysLibrary.cpp:65
LibraryPackage::locateRoutineEntry
RexxRoutineEntry * locateRoutineEntry(RexxString *name)
Definition: LibraryPackage.cpp:329
LibraryUnloaderDispatcher::run
virtual void run()
Definition: LibraryPackage.cpp:517
LibraryPackage::libraryName
RexxString * libraryName
Definition: LibraryPackage.hpp:92
RexxRoutine
Definition: RexxNativeCode.hpp:96
RexxNativeCode.hpp
LibraryPackage::load
bool load()
Definition: LibraryPackage.cpp:136
RexxDirectory::put
RexxObject * put(RexxObject *, RexxString *)
Definition: DirectoryClass.cpp:636
LibraryPackage::package
RexxPackageEntry * package
Definition: LibraryPackage.hpp:91
memory_mark
#define memory_mark(oref)
Definition: RexxMemory.hpp:436
Error_Execution_library_version
#define Error_Execution_library_version
Definition: RexxErrorCodes.h:559
SysLibrary::load
bool load(const char *name)
Definition: SysLibrary.cpp:75
LibraryPackage::resolveMethod
RexxNativeMethod * resolveMethod(RexxString *name)
Definition: LibraryPackage.cpp:359
Error_Execution_library_routine
#define Error_Execution_library_routine
Definition: RexxErrorCodes.h:556
new_directory
RexxDirectory * new_directory()
Definition: DirectoryClass.hpp:101
PREGISTEREDROUTINE
size_t(RexxEntry * PREGISTEREDROUTINE)(const char *, size_t, PCONSTRXSTRING, const char *, PRXSTRING)
Definition: MethodClass.hpp:80
_RexxRoutineEntry::name
const char * name
Definition: oorexxapi.h:180
LibraryPackage::resolveMethodEntry
PNATIVEMETHOD resolveMethodEntry(RexxString *name)
Definition: LibraryPackage.cpp:410
RexxNativeMethod
Definition: RexxNativeCode.hpp:72
LibraryPackage::methods
RexxDirectory * methods
Definition: LibraryPackage.hpp:94
LibraryPackage::liveGeneral
void liveGeneral(int reason)
Definition: LibraryPackage.cpp:109
SysLibrary::reset
void reset()
Definition: SysLibrary.hpp:57
_RexxPackageEntry
Definition: oorexxapi.h:241
REXX_CURRENT_INTERPRETER_VERSION
#define REXX_CURRENT_INTERPRETER_VERSION
Definition: oorexxapi.h:223
LibraryPackage::resolveRegisteredRoutineEntry
PREGISTEREDROUTINE resolveRegisteredRoutineEntry(RexxString *name)
Definition: LibraryPackage.cpp:458
LibraryUnloaderDispatcher
Definition: LibraryPackage.hpp:114
_RexxRoutineEntry::entryPoint
void * entryPoint
Definition: oorexxapi.h:181
_RexxPackageEntry::loader
RexxPackageLoader loader
Definition: oorexxapi.h:248
ActivityManager::currentActivity
static RexxActivity *volatile currentActivity
Definition: ActivityManager.hpp:95
_RexxPackageEntry::methods
struct _RexxMethodEntry * methods
Definition: oorexxapi.h:251
LibraryPackage::getPackageTable
RexxPackageEntry * getPackageTable()
Definition: LibraryPackage.cpp:181
RexxCore.h
LibraryPackage::reload
void reload()
Definition: LibraryPackage.cpp:480
DirectoryClass.hpp
_RexxMethodEntry::entryPoint
void * entryPoint
Definition: oorexxapi.h:202
Interpreter::logicError
static void logicError(const char *desc)
Definition: Interpreter.cpp:553
LibraryPackage::loadRoutines
void loadRoutines(RexxRoutineEntry *table)
Definition: LibraryPackage.cpp:245
new_object
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:422
LibraryPackage::live
void live(size_t liveMark)
Definition: LibraryPackage.cpp:99
new_upper_string
RexxString * new_upper_string(const char *s, stringsize_t l)
Definition: StringClass.hpp:573
_RexxMethodEntry::style
int style
Definition: oorexxapi.h:199
LibraryPackage::routines
RexxDirectory * routines
Definition: LibraryPackage.hpp:93
LibraryPackage::LibraryPackage
LibraryPackage(RexxString *n)
Definition: LibraryPackage.cpp:73
RexxObject
Definition: ObjectClass.hpp:311
PackageManager.hpp
RexxString
Definition: StringClass.hpp:119
_RexxPackageEntry::requiredVersion
int requiredVersion
Definition: oorexxapi.h:245
LibraryPackage::lib
SysLibrary lib
Definition: LibraryPackage.hpp:95
RexxNativeRoutine
Definition: RexxNativeCode.hpp:107