Browse Source

Added classLoader parameter to JavaBytecodeRunUnit#run

kindlich 6 years ago
parent
commit
bfb0a33b27
No known key found for this signature in database

+ 37
- 25
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunUnit.java View File

38
 	private final List<JavaScriptMethod> scripts = new ArrayList<>();
38
 	private final List<JavaScriptMethod> scripts = new ArrayList<>();
39
 	private final List<FunctionParameter> scriptParameters = new ArrayList<>();
39
 	private final List<FunctionParameter> scriptParameters = new ArrayList<>();
40
 	private final List<JavaParameterInfo> scriptParameterInfo = new ArrayList<>();
40
 	private final List<JavaParameterInfo> scriptParameterInfo = new ArrayList<>();
41
-	
41
+
42
 	private boolean scriptsWritten = false;
42
 	private boolean scriptsWritten = false;
43
-	
43
+
44
 	public void add(JavaBytecodeModule module) {
44
 	public void add(JavaBytecodeModule module) {
45
 		scriptsWritten = false;
45
 		scriptsWritten = false;
46
-		
46
+
47
 		for (Map.Entry<String, byte[]> classEntry : module.getClasses().entrySet())
47
 		for (Map.Entry<String, byte[]> classEntry : module.getClasses().entrySet())
48
 			classes.put(classEntry.getKey().replace('/', '.'), classEntry.getValue());
48
 			classes.put(classEntry.getKey().replace('/', '.'), classEntry.getValue());
49
-		
49
+
50
 		for (JavaScriptMethod script : module.getScripts()) {
50
 		for (JavaScriptMethod script : module.getScripts()) {
51
 			scripts.add(script);
51
 			scripts.add(script);
52
-			
52
+
53
 			for (int i = 0; i < script.parameters.length; i++) {
53
 			for (int i = 0; i < script.parameters.length; i++) {
54
 				FunctionParameter parameter = script.parameters[i];
54
 				FunctionParameter parameter = script.parameters[i];
55
 				if (!scriptParameters.contains(parameter)) {
55
 				if (!scriptParameters.contains(parameter)) {
59
 			}
59
 			}
60
 		}
60
 		}
61
 	}
61
 	}
62
-	
62
+
63
 	public void run() {
63
 	public void run() {
64
-		run(Collections.emptyMap());
64
+		run(Collections.emptyMap(), this.getClass().getClassLoader());
65
 	}
65
 	}
66
-	
66
+
67
 	public void run(Map<FunctionParameter, Object> arguments) {
67
 	public void run(Map<FunctionParameter, Object> arguments) {
68
+		run(arguments, this.getClass().getClassLoader());
69
+	}
70
+
71
+	public void run(Map<FunctionParameter, Object> arguments, ClassLoader parentClassLoader) {
68
 		writeScripts();
72
 		writeScripts();
69
-		
70
-		ScriptClassLoader classLoader = new ScriptClassLoader();
73
+
74
+		ScriptClassLoader classLoader = new ScriptClassLoader(parentClassLoader);
71
 
75
 
72
 		Object[] argumentsArray = new Object[scriptParameters.size()];
76
 		Object[] argumentsArray = new Object[scriptParameters.size()];
73
 		for (int i = 0; i < scriptParameters.size(); i++) {
77
 		for (int i = 0; i < scriptParameters.size(); i++) {
74
 			FunctionParameter parameter = scriptParameters.get(i);
78
 			FunctionParameter parameter = scriptParameters.get(i);
75
 			if (!arguments.containsKey(parameter))
79
 			if (!arguments.containsKey(parameter))
76
 				throw new IllegalArgumentException("Missing script argument for parameter " + parameter.name);
80
 				throw new IllegalArgumentException("Missing script argument for parameter " + parameter.name);
77
-			
81
+
78
 			argumentsArray[i] = arguments.get(parameter);
82
 			argumentsArray[i] = arguments.get(parameter);
79
 		}
83
 		}
80
 		try {
84
 		try {
86
 			Logger.getLogger(JavaBytecodeRunUnit.class.getName()).log(Level.SEVERE, null, ex);
90
 			Logger.getLogger(JavaBytecodeRunUnit.class.getName()).log(Level.SEVERE, null, ex);
87
 		}
91
 		}
88
 	}
92
 	}
89
-	
93
+
90
 	public void dump(File directory) {
94
 	public void dump(File directory) {
91
 		writeScripts();
95
 		writeScripts();
92
-		
96
+
93
 		if (!directory.exists())
97
 		if (!directory.exists())
94
 			directory.mkdirs();
98
 			directory.mkdirs();
95
-		
99
+
96
 		for (Map.Entry<String, byte[]> classEntry : classes.entrySet()) {
100
 		for (Map.Entry<String, byte[]> classEntry : classes.entrySet()) {
97
 			File output = new File(directory, classEntry.getKey() + ".class");
101
 			File output = new File(directory, classEntry.getKey() + ".class");
98
 			try (FileOutputStream outputStream = new FileOutputStream(output)) {
102
 			try (FileOutputStream outputStream = new FileOutputStream(output)) {
102
 			}
106
 			}
103
 		}
107
 		}
104
 	}
108
 	}
105
-	
109
+
106
 	private int getParameterIndex(FunctionParameter parameter) {
110
 	private int getParameterIndex(FunctionParameter parameter) {
107
 		return scriptParameters.indexOf(parameter);
111
 		return scriptParameters.indexOf(parameter);
108
 	}
112
 	}
109
-	
113
+
110
 	private void writeScripts() {
114
 	private void writeScripts() {
111
 		if (scriptsWritten)
115
 		if (scriptsWritten)
112
 			return;
116
 			return;
113
-		
117
+
114
 		JavaClassWriter scriptsClassWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
118
 		JavaClassWriter scriptsClassWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
115
 		scriptsClassWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
119
 		scriptsClassWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
116
-		
120
+
117
 		FunctionHeader header = new FunctionHeader(BasicTypeID.VOID, scriptParameters.toArray(new FunctionParameter[scriptParameters.size()]));
121
 		FunctionHeader header = new FunctionHeader(BasicTypeID.VOID, scriptParameters.toArray(new FunctionParameter[scriptParameters.size()]));
118
 		StringBuilder headerBuilder = new StringBuilder();
122
 		StringBuilder headerBuilder = new StringBuilder();
119
 		headerBuilder.append('(');
123
 		headerBuilder.append('(');
121
 			headerBuilder.append(scriptParameterInfo.get(i).typeDescriptor);
125
 			headerBuilder.append(scriptParameterInfo.get(i).typeDescriptor);
122
 		}
126
 		}
123
 		headerBuilder.append(")V");
127
 		headerBuilder.append(")V");
124
-		
128
+
125
 		JavaMethod runMethod = JavaMethod.getStatic(new JavaClass("script", "Scripts", JavaClass.Kind.CLASS), "run", headerBuilder.toString(), Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);
129
 		JavaMethod runMethod = JavaMethod.getStatic(new JavaClass("script", "Scripts", JavaClass.Kind.CLASS), "run", headerBuilder.toString(), Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);
126
 		final JavaWriter runWriter = new JavaWriter(CodePosition.GENERATED, scriptsClassWriter, runMethod, null, null, null);
130
 		final JavaWriter runWriter = new JavaWriter(CodePosition.GENERATED, scriptsClassWriter, runMethod, null, null, null);
127
 		runWriter.start();
131
 		runWriter.start();
131
 			}
135
 			}
132
 			runWriter.invokeStatic(method.method);
136
 			runWriter.invokeStatic(method.method);
133
 		}
137
 		}
134
-		
138
+
135
 		runWriter.ret();
139
 		runWriter.ret();
136
 		runWriter.end();
140
 		runWriter.end();
137
-		
141
+
138
 		classes.put("Scripts", scriptsClassWriter.toByteArray());
142
 		classes.put("Scripts", scriptsClassWriter.toByteArray());
139
 		scriptsWritten = true;
143
 		scriptsWritten = true;
140
 	}
144
 	}
142
 	public class ScriptClassLoader extends ClassLoader {
146
 	public class ScriptClassLoader extends ClassLoader {
143
 		private final Map<String, Class> customClasses = new HashMap<>();
147
 		private final Map<String, Class> customClasses = new HashMap<>();
144
 
148
 
149
+		ScriptClassLoader() {
150
+			super();
151
+		}
152
+
153
+		public ScriptClassLoader(ClassLoader parent) {
154
+			super(parent);
155
+		}
156
+
145
 		@Override
157
 		@Override
146
 		public Class<?> loadClass(String name) throws ClassNotFoundException {
158
 		public Class<?> loadClass(String name) throws ClassNotFoundException {
147
 			//System.out.println("LoadClass " + name);
159
 			//System.out.println("LoadClass " + name);
148
-			
160
+
149
 			if (customClasses.containsKey(name))
161
 			if (customClasses.containsKey(name))
150
 				return customClasses.get(name);
162
 				return customClasses.get(name);
151
 			if (classes.containsKey(name)) {
163
 			if (classes.containsKey(name)) {
156
 			return super.loadClass(name);
168
 			return super.loadClass(name);
157
 		}
169
 		}
158
 	}
170
 	}
159
-	
171
+
160
 	private static Class<?> loadClass(ClassLoader classLoader, String descriptor) throws ClassNotFoundException {
172
 	private static Class<?> loadClass(ClassLoader classLoader, String descriptor) throws ClassNotFoundException {
161
 		switch (descriptor) {
173
 		switch (descriptor) {
162
 			case "Z": return boolean.class;
174
 			case "Z": return boolean.class;
172
 			case "[Ljava/lang/Object;": return Object[].class;
184
 			case "[Ljava/lang/Object;": return Object[].class;
173
 			case "[Ljava/lang/String;": return String[].class;
185
 			case "[Ljava/lang/String;": return String[].class;
174
 		}
186
 		}
175
-		
187
+
176
 		return classLoader.loadClass(getClassName(descriptor));
188
 		return classLoader.loadClass(getClassName(descriptor));
177
 	}
189
 	}
178
-	
190
+
179
 	private static String getClassName(String descriptor) {
191
 	private static String getClassName(String descriptor) {
180
 		if (descriptor.startsWith("[")) {
192
 		if (descriptor.startsWith("[")) {
181
 			return "[" + getClassName(descriptor.substring(1));
193
 			return "[" + getClassName(descriptor.substring(1));

Loading…
Cancel
Save