RexxNativeCode.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.oorexx.org/license.html */
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 RexxNativeMethod.cpp */
40 /* */
41 /* Primitive Method Class */
42 /* */
43 /******************************************************************************/
44 #include <stdlib.h>
45 #include <string.h>
46 #include "RexxCore.h"
47 #include "StringClass.hpp"
48 #include "DirectoryClass.hpp"
49 #include "RexxNativeActivation.hpp"
50 #include "RexxNativeCode.hpp"
51 #include "SourceFile.hpp"
52 #include "PackageManager.hpp"
53 #include <ctype.h>
54 
55 
57 {
58  // and this is the information needed to resolve this again after an
59  // image restore
60  OrefSet(this, this->package, _package);
61  OrefSet(this, this->name, _name);
62  // this will be set later, if available
63  OrefSet(this, this->source, OREF_NULL);
64 }
65 
66 
67 void RexxNativeCode::live(size_t liveMark)
68 /******************************************************************************/
69 /* Function: Normal garbage collection live marking */
70 /******************************************************************************/
71 {
72  memory_mark(this->package);
73  memory_mark(this->name);
74  memory_mark(this->source);
75 }
76 
77 
79 /******************************************************************************/
80 /* Function: Normal garbage collection live marking */
81 /******************************************************************************/
82 {
86 }
87 
88 
90 /******************************************************************************/
91 /* Function: Flatten an object */
92 /******************************************************************************/
93 {
95  flatten_reference(newThis->package, envelope);
96  flatten_reference(newThis->name, envelope);
97  flatten_reference(newThis->source, envelope);
99 }
100 
101 
102 /**
103  * Resolve a class in the context of this code object.
104  *
105  * @param className The name of the desired class.
106  *
107  * @return The returned class.
108  */
110 {
111  // if there is a source object attached, have it resolve things. Otherwise, go back to the default.
112  if (source != OREF_NULL)
113  {
114  return source->findClass(className);
115  }
116  return BaseCode::findClass(className);
117 }
118 
119 
120 /**
121  * Set a source object into a native code context. If the
122  * object is already set, then this returns a copy of the code object.
123  *
124  * @param s The new source object.
125  *
126  * @return Either the same object, or a new copy of the code object.
127  */
129 {
130  if (source == OREF_NULL)
131  {
132  OrefSet(this, this->source, s);
133  return this;
134  }
135  else
136  {
137  RexxNativeCode *codeCopy = (RexxNativeCode *)this->copy();
138  OrefSet(codeCopy, codeCopy->source, s);
139  return codeCopy;
140  }
141 }
142 
143 
144 /**
145  * Get the source object backing this code instance. If created from
146  * a directive, this will be the source package containing the
147  * directive.
148  *
149  * @return The parent source instance.
150  */
152 {
153  return source;
154 }
155 
156 
157 /**
158  * Get the security manager associated with native code. Generally,
159  * only native methods and routines defined with directives
160  * will have an associated security manager.
161  *
162  * @return The source security manager.
163  */
165 {
166  if (source != OREF_NULL)
167  {
168  return source->getSecurityManager();
169  }
170  return OREF_NULL;
171 }
172 
173 
175 /******************************************************************************/
176 /* Function: Generalized object marking */
177 /******************************************************************************/
178 {
179  // zero the entry point if saving the image
180  if (reason == SAVINGIMAGE)
181  {
182  entry = NULL;
183  }
185 }
186 
187 
189 /******************************************************************************/
190 /* Function: Flatten an object */
191 /******************************************************************************/
192 {
194  newThis->entry = NULL;
195  RexxNativeCode::flatten(envelope);
197 }
198 
199 
201 /******************************************************************************/
202 /* Function: Generalized object marking */
203 /******************************************************************************/
204 {
205  // zero the entry point if saving the image
206  if (reason == SAVINGIMAGE)
207  {
208  entry = NULL;
209  }
211 }
212 
213 
215 /******************************************************************************/
216 /* Function: Flatten an object */
217 /******************************************************************************/
218 {
220  newThis->entry = NULL;
221  RexxNativeCode::flatten(envelope);
223 }
224 
225 
227 /******************************************************************************/
228 /* Function: Generalized object marking */
229 /******************************************************************************/
230 {
231  // zero the entry point if saving the image
232  if (reason == SAVINGIMAGE)
233  {
234  entry = NULL;
235  }
237 }
238 
239 
241 /******************************************************************************/
242 /* Function: Flatten an object */
243 /******************************************************************************/
244 {
246  newThis->entry = NULL;
247  RexxNativeCode::flatten(envelope);
249 }
250 
251 
252 /**
253  * Run a method call (vs a straight program call).
254  *
255  * @param activity The current activity.
256  * @param method The method we're attached to.
257  * @param receiver The method receiver object (the "self" object).
258  * @param messageName
259  * The name of the message used to invoke the method.
260  * @param argPtr The pointer to the arguments.
261  * @param count The count of positional arguments.
262  * @param named_count The count of named arguments.
263  * @param result The protected object used to return the result.
264  */
265 void RexxNativeMethod::run(RexxActivity *activity, RexxMethod *method, RexxObject *receiver, RexxString *messageName,
266  RexxObject **argPtr, size_t count, size_t named_count, ProtectedObject &result)
267 {
268  // if this is NULL currently, we need to lazy resolve this entry
269  if (entry == NULL)
270  {
271  // have the package manager resolve this for us before we make a call
273  }
274 
275  // create a new native activation
277  activity->pushStackFrame(newNActa); /* push it on the activity stack */
278  /* and go run it */
279  newNActa->run(method, this, receiver, messageName, argPtr, count, named_count, result);
280 }
281 
282 
283 void * RexxNativeMethod::operator new(
284  size_t size) /* object size */
285 /****************************************************************************/
286 /* Function: Create a new Native method */
287 /****************************************************************************/
288 {
289  return new_object(size, T_NativeMethod); // Get new object
290 }
291 
292 
293 /**
294  * Run a method call (vs a straight program call).
295  *
296  * @param activity The current activity.
297  * @param functionName
298  * The name of the message used to invoke the method.
299  * @param argPtr The pointer to the arguments.
300  * @param count The count of positional arguments.
301  * @param named_count The count of named arguments.
302  * @param result The protected object used to return the result.
303  */
304 void RexxNativeRoutine::call(RexxActivity *activity, RoutineClass *routine, RexxString *functionName, RexxObject **argPtr, size_t count, size_t named_count, ProtectedObject &result)
305 {
306  // if this is NULL currently, we need to lazy resolve this entry
307  if (entry == NULL)
308  {
309  // have the package manager resolve this for us before we make a call
311  }
312 
313  // create a new native activation
315  activity->pushStackFrame(newNActa); /* push it on the activity stack */
316  /* and go run it */
317  newNActa->callNativeRoutine(routine, this, functionName, argPtr, count, named_count, result);
318 }
319 
320 
321 void * RexxNativeRoutine::operator new(
322  size_t size) /* object size */
323 /****************************************************************************/
324 /* Function: Create a new Native method */
325 /****************************************************************************/
326 {
327  return new_object(size, T_NativeRoutine); // Get new object
328 }
329 
330 
331 /**
332  * Run a method call (vs a straight program call).
333  *
334  * @param activity The current activity.
335  * @param functionName
336  * The name of the message used to invoke the method.
337  * @param argPtr The pointer to the arguments.
338  * @param count The count of positional arguments.
339  * @param named_count The count of named arguments.
340  * @param result The protected object used to return the result.
341  */
342 void RegisteredRoutine::call(RexxActivity *activity, RoutineClass *routine, RexxString *functionName, RexxObject **argPtr, size_t count, size_t named_count, ProtectedObject &result)
343 {
344  // if this is NULL currently, we need to lazy resolve this entry
345  if (entry == NULL)
346  {
347  // have the package manager resolve this for us before we make a call
349  }
350 
351  // create a new native activation
353  activity->pushStackFrame(newNActa); /* push it on the activity stack */
354  /* and go run it */
355  newNActa->callRegisteredRoutine(routine, this, functionName, argPtr, count, named_count, result);
356 }
357 
358 
359 void * RegisteredRoutine::operator new(
360  size_t size) /* object size */
361 /****************************************************************************/
362 /* Function: Create a new Native method */
363 /****************************************************************************/
364 {
365  return new_object(size, T_RegisteredRoutine); // Get new object
366 }
@ T_NativeRoutine
@ T_NativeMethod
@ T_RegisteredRoutine
#define OREF_NULL
Definition: RexxCore.h:60
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
#define memory_mark(oref)
Definition: RexxMemory.hpp:445
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:431
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:493
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:446
@ SAVINGIMAGE
Definition: RexxMemory.hpp:117
#define cleanUpFlatten
Definition: RexxMemory.hpp:479
#define setUpFlatten(type)
Definition: RexxMemory.hpp:473
static RexxNativeActivation * newNativeActivation(RexxActivity *activity, RexxActivation *parent)
virtual RexxClass * findClass(RexxString *className)
static PNATIVEROUTINE resolveRoutineEntry(RexxString *package, RexxString *name)
static PREGISTEREDROUTINE resolveRegisteredRoutineEntry(RexxString *package, RexxString *name)
static PNATIVEMETHOD resolveMethodEntry(RexxString *package, RexxString *name)
void liveGeneral(int reason)
void flatten(RexxEnvelope *envelope)
RexxRoutineHandler * entry
virtual void call(RexxActivity *, RoutineClass *, RexxString *, RexxObject **, size_t, size_t, ProtectedObject &)
void pushStackFrame(RexxActivationBase *new_activation)
virtual RexxObject * copy()
void callRegisteredRoutine(RoutineClass *routine, RegisteredRoutine *code, RexxString *functionName, RexxObject **list, size_t count, size_t named_count, ProtectedObject &resultObj)
void callNativeRoutine(RoutineClass *routine, RexxNativeRoutine *code, RexxString *functionName, RexxObject **list, size_t count, size_t named_count, ProtectedObject &result)
void run(RexxMethod *_method, RexxNativeMethod *_code, RexxObject *_receiver, RexxString *_msgname, RexxObject **_arglist, size_t _argcount, size_t _named_argcount, ProtectedObject &resultObj)
virtual RexxClass * findClass(RexxString *className)
RexxString * name
SecurityManager * getSecurityManager()
virtual RexxSource * getSourceObject()
virtual BaseCode * setSourceObject(RexxSource *s)
RexxString * package
void live(size_t)
void flatten(RexxEnvelope *envelope)
RexxSource * source
void liveGeneral(int reason)
PNATIVEMETHOD entry
void liveGeneral(int reason)
virtual void run(RexxActivity *activity, RexxMethod *method, RexxObject *receiver, RexxString *messageName, RexxObject **argPtr, size_t count, size_t named_count, ProtectedObject &result)
void flatten(RexxEnvelope *envelope)
virtual void call(RexxActivity *, RoutineClass *, RexxString *, RexxObject **, size_t, size_t, ProtectedObject &)
PNATIVEROUTINE entry
void flatten(RexxEnvelope *envelope)
void liveGeneral(int reason)
SecurityManager * getSecurityManager()
Definition: SourceFile.hpp:379
RexxClass * findClass(RexxString *)