ef293f6b58a1d0ed699f848f8562c5b5d1885c2c
[cacao.git] / src / vm / package.cpp
1 /* src/vm/package.cpp - Java boot-package functions
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "toolbox/list.h"
31
32 #include "mm/memory.h"
33
34 #include "native/jni.h"
35
36 #include "vm/package.hpp"
37 #include "vm/string.hpp"
38
39 #include "vmcore/options.h"
40 #include "vmcore/utf8.h"
41
42
43 /* internal property structure ************************************************/
44
45 typedef struct list_package_entry_t list_package_entry_t;
46
47 struct list_package_entry_t {
48 /*      java_string_t *packagename; */
49         utf           *packagename;
50         listnode_t     linkage;
51 };
52
53
54 /* global variables ***********************************************************/
55
56 static list_t *list_package = NULL;
57
58
59 /**
60  * Initialize the package list.
61  */
62 void Package::initialize(void)
63 {
64         TRACESUBSYSTEMINITIALIZATION("package_init");
65
66         /* create the properties list */
67
68         list_package = list_create(OFFSET(list_package_entry_t, linkage));
69 }
70
71
72 /**
73  * Add a package to the boot-package list.
74  *
75  * @param packagename Package name as Java string.
76  */
77 /* void package_add(java_handle_t *packagename) */
78 void Package::add(utf *packagename)
79 {
80 /*      java_string_t        *s; */
81         list_package_entry_t *lpe;
82
83         /* Intern the Java string to get a unique address. */
84
85 /*      s = javastring_intern(packagename); */
86
87         /* Check if the package is already stored. */
88
89         if (Package::find(packagename) != NULL)
90                 return;
91
92         /* Add the package. */
93
94 #if !defined(NDEBUG)
95         if (opt_DebugPackage) {
96                 log_start();
97                 log_print("[package_add: packagename=");
98                 utf_display_printable_ascii(packagename);
99                 log_print("]");
100                 log_finish();
101         }
102 #endif
103
104         lpe = NEW(list_package_entry_t);
105
106         lpe->packagename = packagename;
107
108         list_add_last(list_package, lpe);
109 }
110
111
112 /**
113  * Find a package in the list.
114  *
115  * @param packagename Package name as Java string.
116  *
117  * @return Package name as Java string.
118  */
119 /* java_handle_t *package_find(java_handle_t *packagename) */
120 utf* Package::find(utf *packagename)
121 {
122 /*      java_string_t        *s; */
123         list_t               *l;
124         list_package_entry_t *lpe;
125
126         /* Intern the Java string to get a unique address. */
127
128 /*      s = javastring_intern(packagename); */
129
130         /* For convenience. */
131
132         l = list_package;
133
134         for (lpe = (list_package_entry_t*) list_first(l); lpe != NULL; lpe = (list_package_entry_t*) list_next(l, lpe)) {
135 /*              if (lpe->packagename == s) */
136                 if (lpe->packagename == packagename)
137                         return lpe->packagename;
138         }
139
140         return NULL;
141 }
142
143
144 /* Legacy C interface *********************************************************/
145
146 extern "C" {
147
148 void Package_initialize(void) { Package::initialize(); }
149 void Package_add(utf* packagename) { Package::add(packagename); }
150 utf* Package_find(utf *packagename) { return Package::find(packagename); }
151
152 }
153
154
155 /*
156  * These are local overrides for various environment variables in Emacs.
157  * Please do not remove this and leave it at the end of the file, where
158  * Emacs will automagically detect them.
159  * ---------------------------------------------------------------------
160  * Local variables:
161  * mode: c++
162  * indent-tabs-mode: t
163  * c-basic-offset: 4
164  * tab-width: 4
165  * End:
166  * vim:noexpandtab:sw=4:ts=4:
167  */