Skip to content

Commit

Permalink
JSS-110 Implement charCodeAt for the String prototype
Browse files Browse the repository at this point in the history
Example Code:
```js
"abc".charCodeAt(0); // Returns 97
"abc".chatCodeAt(-1); // Returns NaN

var obj = new String("abc");
obj.charCodeAt(0); // Returns 97
obj.chatCodeAt(-1); // Returns NaN
```
  • Loading branch information
PrestonLTaylor committed Jun 4, 2024
1 parent 7788614 commit 0de507d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions JSS.Lib/Runtime/String.prototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public void Initialize(Realm realm, VM vm)
// 22.1.3.2 String.prototype.charAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.charat
InternalDefineProperty(vm, "charAt", 1, charAt, new(true, false, true));

// 22.1.3.3 String.prototype.charCodeAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.charcodeat
InternalDefineProperty(vm, "charCodeAt", 1, charCodeAt, new(true, false, true));

// 22.1.3.6 String.prototype.constructor, The initial value of String.prototype.constructor is %String%.
InternalDefineProperty("constructor", realm.StringConstructor, new(true, false, true));

Expand Down Expand Up @@ -96,6 +99,31 @@ private Completion charAt(VM vm, Value thisValue, List argumentList, Object newT
return S.Value.Substring((int)position.Value, 1);
}

// 22.1.3.3 String.prototype.charCodeAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.charcodeat
private Completion charCodeAt(VM vm, Value thisValue, List argumentList, Object newTarget)
{
// 1. Let O be ? RequireObjectCoercible(this value).
var O = thisValue.RequireObjectCoercible(vm);
if (O.IsAbruptCompletion()) return O;

// 2. Let S be ? ToString(O).
var S = O.Value.ToStringJS(vm);
if (S.IsAbruptCompletion()) return S.Completion;

// 3. Let position be ? ToIntegerOrInfinity(pos).
var position = argumentList[0].ToIntegerOrInfinity(vm);
if (position.IsAbruptCompletion()) return position.Completion;

// 4. Let size be the length of S.
var size = S.Value.Length;

// 5. If position < 0 or position ≥ size, return NaN.
if (position.Value < 0 || position.Value >= size) return double.NaN;

// 6. Return the Number value for the numeric value of the code unit at index position within the String S.
return S.Value[(int)position.Value];
}

// 22.1.3.28 String.prototype.toLowerCase ( ), https://tc39.es/ecma262/multipage/text-processing.html#sec-string.prototype.tolowercase
private Completion toLowerCase(VM vm, Value thisValue, List argumentList, Object newTarget)
{
Expand Down

0 comments on commit 0de507d

Please sign in to comment.