Browse Source

Added loadMethod utility method for loading in a Method and retrieving its reference.

Stan Hebben 6 years ago
parent
commit
ae81e5966e

+ 14
- 2
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

@@ -14,9 +14,7 @@ import java.lang.reflect.Type;
14 14
 import java.lang.reflect.TypeVariable;
15 15
 import java.util.Collections;
16 16
 import java.util.HashMap;
17
-import java.util.HashSet;
18 17
 import java.util.Map;
19
-import java.util.Set;
20 18
 import org.openzen.zencode.shared.CodePosition;
21 19
 import org.openzen.zenscript.codemodel.FunctionHeader;
22 20
 import org.openzen.zenscript.codemodel.FunctionParameter;
@@ -195,6 +193,20 @@ public class JavaNativeModule {
195 193
 		}
196 194
 	}
197 195
 	
196
+	public FunctionalMemberRef loadStaticMethod(Method method) {
197
+		if (!isStatic(method.getModifiers()))
198
+			throw new IllegalArgumentException("Method is not static");
199
+		
200
+		HighLevelDefinition definition = addClass(method.getDeclaringClass());
201
+		JavaClass jcls = JavaClass.fromInternalName(getInternalName(method.getDeclaringClass()), JavaClass.Kind.CLASS);
202
+		
203
+		MethodMember methodMember = new MethodMember(CodePosition.NATIVE, definition, Modifiers.PUBLIC | Modifiers.STATIC, method.getName(), getHeader(method), null);
204
+		definition.addMember(methodMember);
205
+		boolean isGenericResult = methodMember.header.getReturnType().isGeneric();
206
+		compiled.setMethodInfo(methodMember, new JavaMethod(jcls, JavaMethod.Kind.STATIC, method.getName(), false, getMethodDescriptor(method), method.getModifiers(), isGenericResult));
207
+		return methodMember.ref(registry.getForDefinition(definition).stored());
208
+	}
209
+	
198 210
 	private ZSPackage getPackage(String className) {
199 211
 		if (!className.contains("."))
200 212
 			return pkg;

+ 0
- 2
Parser/src/main/java/org/openzen/zenscript/parser/PrefixedBracketParser.java View File

@@ -8,8 +8,6 @@ package org.openzen.zenscript.parser;
8 8
 import java.util.HashMap;
9 9
 import java.util.Map;
10 10
 import org.openzen.zencode.shared.CodePosition;
11
-import org.openzen.zencode.shared.CompileException;
12
-import org.openzen.zencode.shared.CompileExceptionCode;
13 11
 import org.openzen.zenscript.lexer.ParseException;
14 12
 import org.openzen.zenscript.lexer.ZSToken;
15 13
 import org.openzen.zenscript.lexer.ZSTokenParser;

Parser/src/main/java/org/openzen/zenscript/parser/SimpleBracketSubParser.java → Parser/src/main/java/org/openzen/zenscript/parser/SimpleBracketParser.java View File

@@ -23,11 +23,11 @@ import org.openzen.zenscript.parser.expression.ParsedExpression;
23 23
  *
24 24
  * @author Hoofdgebruiker
25 25
  */
26
-public class SimpleBracketSubParser implements BracketExpressionParser {
26
+public class SimpleBracketParser implements BracketExpressionParser {
27 27
 	private final FunctionalMemberRef method;
28 28
 	private final TypeID targetType;
29 29
 	
30
-	public SimpleBracketSubParser(GlobalTypeRegistry registry, FunctionalMemberRef method) {
30
+	public SimpleBracketParser(GlobalTypeRegistry registry, FunctionalMemberRef method) {
31 31
 		if (!method.isStatic())
32 32
 			throw new IllegalArgumentException("Method must be static");
33 33
 		if (method.getHeader().getNumberOfTypeParameters() > 0)

+ 4
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Globals.java View File

@@ -36,4 +36,8 @@ public class Globals implements ZenCodeGlobals {
36 36
 		for (TestClass object : objects)
37 37
 			System.out.println("  - " + object.getName());
38 38
 	}
39
+	
40
+	public static String bracket(String value) {
41
+		return "BRACKET:" + value;
42
+	}
39 43
 }

+ 4
- 23
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

@@ -2,13 +2,11 @@ package org.openzen.zenscript.scriptingexample;
2 2
 
3 3
 import java.io.File;
4 4
 import java.io.IOException;
5
-import java.util.Collections;
6 5
 import java.util.HashMap;
7 6
 import java.util.Map;
8 7
 import java.util.Optional;
9 8
 import org.openzen.zencode.java.JavaNativeModule;
10 9
 import org.openzen.zencode.java.ScriptingEngine;
11
-import org.openzen.zencode.shared.CodePosition;
12 10
 import org.openzen.zencode.shared.CompileException;
13 11
 import org.openzen.zencode.shared.FileSourceFile;
14 12
 import org.openzen.zencode.shared.SourceFile;
@@ -16,15 +14,11 @@ import org.openzen.zenscript.codemodel.FunctionParameter;
16 14
 import org.openzen.zenscript.codemodel.SemanticModule;
17 15
 import org.openzen.zenscript.codemodel.type.StringTypeID;
18 16
 import org.openzen.zenscript.lexer.ParseException;
19
-import org.openzen.zenscript.lexer.ZSToken;
20
-import org.openzen.zenscript.lexer.ZSTokenParser;
21
-import org.openzen.zenscript.lexer.ZSTokenType;
22 17
 import org.openzen.zenscript.parser.BracketExpressionParser;
23
-import org.openzen.zenscript.parser.expression.ParsedExpression;
24
-import org.openzen.zenscript.parser.expression.ParsedExpressionString;
18
+import org.openzen.zenscript.parser.SimpleBracketParser;
25 19
 
26 20
 public class Main {
27
-	public static void main(String[] args) throws CompileException, ParseException, IOException {
21
+	public static void main(String[] args) throws CompileException, ParseException, IOException, NoSuchMethodException {
28 22
 		ScriptingEngine scriptingEngine = new ScriptingEngine();
29 23
 		scriptingEngine.debug = true;
30 24
 		
@@ -39,8 +33,9 @@ public class Main {
39 33
 		for (int i = 0; i < inputFiles.length; i++)
40 34
 			sourceFiles[i] = new FileSourceFile(inputFiles[i].getName(), inputFiles[i]);
41 35
 		
36
+		BracketExpressionParser bracketParser = new SimpleBracketParser(scriptingEngine.registry, example.loadStaticMethod(Globals.class.getMethod("bracket", String.class)));
42 37
 		FunctionParameter parameter = new FunctionParameter(scriptingEngine.registry.getArray(StringTypeID.AUTO, 1).stored(), "args");
43
-		SemanticModule scripts = scriptingEngine.createScriptedModule("script", sourceFiles, new TestBracketParser(), new FunctionParameter[] { parameter });
38
+		SemanticModule scripts = scriptingEngine.createScriptedModule("script", sourceFiles, bracketParser, new FunctionParameter[] { parameter });
44 39
 		if (!scripts.isValid())
45 40
 			return;
46 41
 		
@@ -50,18 +45,4 @@ public class Main {
50 45
 		scriptArgs.put(parameter, new String[] { "hello", "world", "example" });
51 46
 		scriptingEngine.run(scriptArgs);
52 47
 	}
53
-	
54
-	private static class TestBracketParser implements BracketExpressionParser {
55
-		@Override
56
-		public ParsedExpression parse(CodePosition position, ZSTokenParser tokens) throws ParseException {
57
-			StringBuilder result = new StringBuilder();
58
-			while (tokens.optional(ZSTokenType.T_GREATER) == null) {
59
-				ZSToken token = tokens.next();
60
-				result.append(token.content);
61
-				result.append(tokens.getLastWhitespace());
62
-			}
63
-			
64
-			return new ParsedExpressionString(position.until(tokens.getPosition()), result.toString(), false);
65
-		}
66
-	}
67 48
 }

+ 1
- 1
common.gradle View File

@@ -6,7 +6,7 @@ apply plugin: 'java'
6 6
 apply plugin: 'maven'
7 7
 
8 8
 String mavenGroupId = 'org.openzen.zencode'
9
-String mavenVersion = '0.3.3'
9
+String mavenVersion = '0.3.5'
10 10
 
11 11
 sourceCompatibility = '1.8'
12 12
 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

Loading…
Cancel
Save