-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GraalJS regular expression and factory implementation (#1058)
- Loading branch information
1 parent
b063972
commit 3daa5bc
Showing
23 changed files
with
954 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/networknt/schema/regex/ECMAScriptRegularExpressionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.regex; | ||
|
||
import com.networknt.schema.utils.Classes; | ||
|
||
/** | ||
* ECMAScript regular expression factory that chooses between GraalJS or Joni | ||
* implementations depending on which is on the classpath. | ||
*/ | ||
public class ECMAScriptRegularExpressionFactory implements RegularExpressionFactory { | ||
private static final boolean JONI_PRESENT = Classes.isPresent("org.joni.Regex", | ||
ECMAScriptRegularExpressionFactory.class.getClassLoader()); | ||
private static final boolean GRAALJS_PRESENT = Classes.isPresent("com.oracle.truffle.js.parser.GraalJSEvaluator", | ||
ECMAScriptRegularExpressionFactory.class.getClassLoader()); | ||
|
||
private static final RegularExpressionFactory DELEGATE = GRAALJS_PRESENT | ||
? GraalJSRegularExpressionFactory.getInstance() | ||
: JoniRegularExpressionFactory.getInstance(); | ||
|
||
public static final ECMAScriptRegularExpressionFactory INSTANCE = new ECMAScriptRegularExpressionFactory(); | ||
|
||
public static ECMAScriptRegularExpressionFactory getInstance() { | ||
if (!JONI_PRESENT && !GRAALJS_PRESENT) { | ||
throw new IllegalArgumentException( | ||
"Either org.jruby.joni:joni or org.graalvm.js:js needs to be present in the classpath"); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public RegularExpression getRegularExpression(String regex) { | ||
return DELEGATE.getRegularExpression(regex); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/networknt/schema/regex/GraalJSContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.regex; | ||
|
||
import org.graalvm.polyglot.Context; | ||
|
||
/** | ||
* Factory for the js {@link Context}. | ||
*/ | ||
public class GraalJSContextFactory { | ||
/** | ||
* The holder defers the classloading until it is used. | ||
*/ | ||
private static class Holder { | ||
private static final Context INSTANCE = Context.newBuilder("js").option("engine.WarnInterpreterOnly", "false") | ||
.build(); | ||
} | ||
|
||
/** | ||
* Gets the singleton instance of the Context. | ||
* <p> | ||
* This may need to be closed to release resources if no longer needed. | ||
* | ||
* @return the Context | ||
*/ | ||
public static Context getInstance() { | ||
return Holder.INSTANCE; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/networknt/schema/regex/GraalJSRegularExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.regex; | ||
|
||
import org.graalvm.polyglot.Value; | ||
|
||
/** | ||
* GraalJS {@link RegularExpression}. | ||
* <p> | ||
* This requires a dependency on org.graalvm.js:js which along with its | ||
* dependency libraries are 50 mb. | ||
*/ | ||
class GraalJSRegularExpression implements RegularExpression { | ||
private final GraalJSRegularExpressionContext context; | ||
private final Value function; | ||
|
||
GraalJSRegularExpression(String regex, GraalJSRegularExpressionContext context) { | ||
this.context = context; | ||
synchronized(context.getContext()) { | ||
this.function = context.getRegExpBuilder().execute(regex); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean matches(String value) { | ||
synchronized(context.getContext()) { | ||
return !function.execute(value).isNull(); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/networknt/schema/regex/GraalJSRegularExpressionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.regex; | ||
|
||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Value; | ||
|
||
/** | ||
* GraalJSRegularExpressionContext. | ||
*/ | ||
public class GraalJSRegularExpressionContext { | ||
private static final String SOURCE = "pattern => {\n" | ||
+ " const regex = new RegExp(pattern, 'u');\n" | ||
+ " return text => text.match(regex)\n" | ||
+ "};"; | ||
|
||
private final Context context; | ||
private final Value regExpBuilder; | ||
|
||
/** | ||
* Constructor. | ||
* <p> | ||
* It is the caller's responsibility to release the context when it is no longer | ||
* required. | ||
* | ||
* @param context the context | ||
*/ | ||
public GraalJSRegularExpressionContext(Context context) { | ||
this.context = context; | ||
synchronized(this.context) { | ||
this.regExpBuilder = this.context.eval("js", SOURCE); | ||
} | ||
} | ||
|
||
/** | ||
* Operations must synchronize on the {@link Context} as only a single thread | ||
* can access the {@link Context} and {@link #getRegExpBuilder()} at one time. | ||
* | ||
* @return the context | ||
*/ | ||
public Context getContext() { | ||
return context; | ||
} | ||
|
||
/** | ||
* Gets the RegExp builder. | ||
* | ||
* @return the regexp builder | ||
*/ | ||
public Value getRegExpBuilder() { | ||
return regExpBuilder; | ||
} | ||
} |
Oops, something went wrong.