Browse Source

Minor array index conversion fixes for 32-bit platforms.

Dmitry Panov 5 years ago
parent
commit
6567f6ffae
5 changed files with 12 additions and 8 deletions
  1. 0 3
      array.go
  2. 3 0
      builtin_typedarrays.go
  3. 4 5
      object_goslice.go
  4. 1 0
      value.go
  5. 4 0
      vm.go

+ 0 - 3
array.go

@@ -622,9 +622,6 @@ func strToGoIdx(s string) int {
 		return int(strToIdx64(s))
 		return int(strToIdx64(s))
 	}
 	}
 	i := strToIdx(s)
 	i := strToIdx(s)
-	if i == math.MaxUint32 {
-		return -1
-	}
 	if i >= math.MaxInt32 {
 	if i >= math.MaxInt32 {
 		return -1
 		return -1
 	}
 	}

+ 3 - 0
builtin_typedarrays.go

@@ -676,6 +676,9 @@ func (r *Runtime) typedArrayProto_lastIndexOf(call FunctionCall) Value {
 				fromIndex = min(fromIndex, length-1)
 				fromIndex = min(fromIndex, length-1)
 			} else {
 			} else {
 				fromIndex += length
 				fromIndex += length
+				if fromIndex < 0 {
+					fromIndex = -1 // prevent underflow in toInt() on 32-bit platforms
+				}
 			}
 			}
 		}
 		}
 
 

+ 4 - 5
object_goslice.go

@@ -135,11 +135,10 @@ func (o *objectGoSlice) putIdx(idx int, v Value, throw bool) {
 }
 }
 
 
 func toInt(i int64) int {
 func toInt(i int64) int {
-	if bits.UintSize == 64 {
-		return int(i)
-	}
-	if i >= math.MaxInt32 {
-		panic(typeError("Integer value overflows 32-bit int"))
+	if bits.UintSize == 32 {
+		if i >= math.MaxInt32 || i < math.MinInt32 {
+			panic(rangeError("Integer value overflows 32-bit int"))
+		}
 	}
 	}
 	return int(i)
 	return int(i)
 }
 }

+ 1 - 0
value.go

@@ -61,6 +61,7 @@ type valueContainer interface {
 }
 }
 
 
 type typeError string
 type typeError string
+type rangeError string
 
 
 type valueInt int64
 type valueInt int64
 type valueFloat float64
 type valueFloat float64

+ 4 - 0
vm.go

@@ -389,6 +389,10 @@ func (vm *vm) try(f func()) (ex *Exception) {
 				ex = &Exception{
 				ex = &Exception{
 					val: vm.r.NewTypeError(string(x1)),
 					val: vm.r.NewTypeError(string(x1)),
 				}
 				}
+			case rangeError:
+				ex = &Exception{
+					val: vm.r.newError(vm.r.global.RangeError, string(x1)),
+				}
 			default:
 			default:
 				/*
 				/*
 					if vm.prg != nil {
 					if vm.prg != nil {