Add some core APIs

This commit is contained in:
Alexey Andreev 2015-09-27 18:49:41 +03:00
parent aa42070311
commit c1389dec25
4 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,83 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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 org.teavm.jso.core;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public abstract class JSRegExp implements JSObject {
@JSBody(params = "pattern", script = "return new RegExp(pattern);")
public static native JSRegExp create(String pattern);
@JSBody(params = { "pattern", "flags" }, script = "return new RegExp(pattern, flags);")
public static native JSRegExp create(String pattern, String flags);
public static JSRegExp create(String pattern, JSRegExpFlag... flags) {
boolean global = false;
boolean ignoreCase = false;
boolean multiline = false;
for (JSRegExpFlag flag : flags) {
switch (flag) {
case GLOBAL:
global = true;
break;
case IGNORE_CASE:
ignoreCase = true;
break;
case MULTILINE:
multiline = true;
break;
}
}
StringBuilder sb = new StringBuilder();
if (global) {
sb.append('g');
}
if (ignoreCase) {
sb.append('i');
}
if (multiline) {
sb.append('m');
}
return create(pattern, sb.toString());
}
@JSProperty
public abstract boolean isGlobal();
@JSProperty
public abstract boolean isIgnoreCase();
@JSProperty
public abstract boolean isMultiline();
@JSProperty
public abstract int getLastIndex();
@JSProperty
public abstract JSString getSource();
public abstract JSArray<JSString> exec(JSString text);
public abstract boolean test(JSString text);
public abstract boolean test(String text);
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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 org.teavm.jso.core;
/**
*
* @author Alexey Andreev
*/
public enum JSRegExpFlag {
GLOBAL,
IGNORE_CASE,
MULTILINE
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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 org.teavm.jso.core;
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
@JSFunctor
public interface JSReplaceFunction extends JSObject {
String apply(String matched);
}

View File

@ -57,12 +57,32 @@ public abstract class JSString implements JSObject {
public abstract int lastIndexOf(JSString a);
public abstract JSArray<JSString> match(JSRegExp regexp);
public abstract JSArray<JSString> match(JSString regexp);
public abstract JSString replace(JSRegExp regexp, JSString replaceBy);
public abstract JSString replace(JSRegExp regexp, JSReplaceFunction replaceBy);
public abstract JSString replace(JSString regexp, JSString replaceBy);
public abstract JSString replace(JSString regexp, JSReplaceFunction replaceBy);
public abstract int search(JSRegExp regexp);
public abstract int search(JSString regexp);
public abstract JSString slice(int beginSlice);
public abstract JSString slice(int beginSlice, int endSlice);
public abstract JSString[] split(JSRegExp separator);
public abstract JSString[] split(JSString separator);
public abstract JSString[] split(JSRegExp separator, int limit);
public abstract JSString[] split(JSString separator, int limit);
public abstract JSString substr(int start);
@ -73,6 +93,12 @@ public abstract class JSString implements JSObject {
public abstract JSString substring(int start, int end);
public abstract JSString toLowerCase();
public abstract JSString toUpperCase();
public abstract JSString trim();
@JSBody(params = "obj", script = "return typeof this === 'string';")
public static native boolean isInstance(JSObject obj);
}