2015-02-16 13:28:33 +00:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegExp
|
|
|
|
|
|
|
|
func builtinRegExp(call FunctionCall) Value {
|
|
|
|
pattern := call.Argument(0)
|
|
|
|
flags := call.Argument(1)
|
|
|
|
if object := pattern._object(); object != nil {
|
|
|
|
if object.class == "RegExp" && flags.IsUndefined() {
|
|
|
|
return pattern
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toValue_object(call.runtime.newRegExp(pattern, flags))
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
func builtinNewRegExp(self *_object, argumentList []Value) Value {
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_object(self.runtime.newRegExp(
|
|
|
|
valueOfArrayIndex(argumentList, 0),
|
|
|
|
valueOfArrayIndex(argumentList, 1),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinRegExp_toString(call FunctionCall) Value {
|
|
|
|
thisObject := call.thisObject()
|
2015-03-20 12:22:01 +00:00
|
|
|
source := thisObject.get("source").string()
|
2015-02-16 13:28:33 +00:00
|
|
|
flags := []byte{}
|
2015-03-20 12:22:01 +00:00
|
|
|
if thisObject.get("global").bool() {
|
2015-02-16 13:28:33 +00:00
|
|
|
flags = append(flags, 'g')
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
if thisObject.get("ignoreCase").bool() {
|
2015-02-16 13:28:33 +00:00
|
|
|
flags = append(flags, 'i')
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
if thisObject.get("multiline").bool() {
|
2015-02-16 13:28:33 +00:00
|
|
|
flags = append(flags, 'm')
|
|
|
|
}
|
|
|
|
return toValue_string(fmt.Sprintf("/%s/%s", source, flags))
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinRegExp_exec(call FunctionCall) Value {
|
|
|
|
thisObject := call.thisObject()
|
2015-03-20 12:22:01 +00:00
|
|
|
target := call.Argument(0).string()
|
2015-02-16 13:28:33 +00:00
|
|
|
match, result := execRegExp(thisObject, target)
|
|
|
|
if !match {
|
2015-03-20 12:22:01 +00:00
|
|
|
return nullValue
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
return toValue_object(execResultToArray(call.runtime, target, result))
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinRegExp_test(call FunctionCall) Value {
|
|
|
|
thisObject := call.thisObject()
|
2015-03-20 12:22:01 +00:00
|
|
|
target := call.Argument(0).string()
|
2015-02-16 13:28:33 +00:00
|
|
|
match, _ := execRegExp(thisObject, target)
|
|
|
|
return toValue_bool(match)
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinRegExp_compile(call FunctionCall) Value {
|
|
|
|
// This (useless) function is deprecated, but is here to provide some
|
|
|
|
// semblance of compatibility.
|
|
|
|
// Caveat emptor: it may not be around for long.
|
2015-03-20 12:22:01 +00:00
|
|
|
return Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|