1
0
mirror of https://github.com/robertkrimen/otto synced 2025-09-28 18:45:22 +08:00

Sometimes RegExp captures can fail, detect this

Fix #2 (In .exec so far)
This commit is contained in:
Robert Krimen 2012-10-14 21:44:55 -07:00
parent 89e26f042e
commit a12391834e
3 changed files with 17 additions and 2 deletions

View File

@ -2,7 +2,6 @@
export TERST_BASE=$(PWD)
TEST := -v --run RegExp
TEST := -v --run Otto
TEST := -v --run underscore
TEST := -v --run underscoreCollection
@ -20,6 +19,7 @@ TEST := -v --run OttoError
TEST := -v --run API
TEST := -v --run IsValidRegExp
TEST := -v --run ParseFailure
TEST := -v --run RegExp
TEST := .
test: test-i

View File

@ -1468,6 +1468,17 @@ func TestRegExp(t *testing.T) {
test(`/abc/.toString()`, "/abc/")
test(`/abc/gim.toString()`, "/abc/gim")
test(`""+/abc/gi`, "/abc/gi")
result := test(`/(a)?/.exec('b')`, ",")
Is(result._object().Get("0"), "")
Is(result._object().Get("1"), "undefined")
Is(result._object().Get("length"), "2")
result = test(`/(a)?(b)?/.exec('b')`, "b,,b")
Is(result._object().Get("0"), "b")
Is(result._object().Get("1"), "undefined")
Is(result._object().Get("2"), "b")
Is(result._object().Get("length"), "3")
}
func TestnewFunction(t *testing.T) {

View File

@ -89,7 +89,11 @@ func execResultToArray(runtime *_runtime, target string, result []int) *_object
valueArray := make([]Value, captureCount)
for index := 0; index < captureCount; index++ {
offset := 2 * index
valueArray[index] = toValue(target[result[offset]:result[offset+1]])
if result[offset] != -1 {
valueArray[index] = toValue(target[result[offset]:result[offset+1]])
} else {
valueArray[index] = UndefinedValue()
}
}
return runtime.newArray(valueArray)
}