Skip to content

Commit

Permalink
Implement String.ToCharArray()
Browse files Browse the repository at this point in the history
Implement Dictionary.get_Keys and Dictionary.get_Values
  • Loading branch information
kg committed Mar 27, 2012
1 parent e875843 commit 9af36b6
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 42 deletions.
2 changes: 1 addition & 1 deletion JSIL/Transforms/EmulateStructAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace JSIL.Transforms {
public class EmulateStructAssignment : JSAstVisitor {
public const bool Tracing = true;
public const bool Tracing = false;

public readonly CLRSpecialIdentifiers CLR;
public readonly IFunctionSource FunctionSource;
Expand Down
40 changes: 29 additions & 11 deletions Libraries/JSIL.Bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,19 @@ JSIL.ImplementExternals("System.Collections.Generic.Stack`1", true, {
}
});

JSIL.MakeClass("System.Object", "JSIL.EnumerableArray", true, [], function ($) {
$.prototype._ctor = function (array) {
this.array = array;
};
$.prototype.GetEnumerator = function () {
return new JSIL.ArrayEnumerator(this.array);
};

JSIL.ImplementInterfaces($, [
System.Collections.IEnumerable, System.Collections.Generic.IEnumerable$b1
]);
});

JSIL.MakeClass("System.Object", "System.Collections.Generic.List`1", true, ["T"], function ($) {
JSIL.ExternalMembers($, true,
"_ctor", "Add", "AddRange", "Remove", "RemoveAt", "Clear",
Expand Down Expand Up @@ -1149,8 +1162,6 @@ JSIL.ImplementExternals(
}
);

JSIL.MakeClass("System.Object", "System.Collections.Generic.Dictionary`2", true, ["TKey", "TValue"]);

JSIL.ImplementExternals(
"System.Collections.Generic.Dictionary`2", true, {
_ctor$0: function () {
Expand All @@ -1173,6 +1184,20 @@ JSIL.ImplementExternals(

this._dict[_key] = value;
},
get_Values: function () {
var keys = Object.keys(this._dict);
var values = new Array(keys.length);

for (var i = 0; i < keys.length; i++)
values[i] = this._dict[keys[i]];

return new JSIL.EnumerableArray(values);
},
get_Keys: function () {
var keys = Object.keys(this._dict);

return new JSIL.EnumerableArray(keys);
},
get_Count: function () {
return this._count;
},
Expand All @@ -1196,6 +1221,8 @@ JSIL.ImplementExternals(
}
);

JSIL.MakeClass("System.Object", "System.Collections.Generic.Dictionary`2", true, ["TKey", "TValue"]);

JSIL.MakeStaticClass("System.Linq.Enumerable", true, [], function ($) {
JSIL.ExternalMembers($, false,
"Count$b1$0"
Expand Down Expand Up @@ -1344,15 +1371,6 @@ JSIL.ImplementExternals(
}
);

JSIL.CompareNumbers = function (lhs, rhs) {
if (lhs > rhs)
return 1;
else if (lhs < rhs)
return -1;
else
return 0;
};

JSIL.ImplementExternals(
"System.Diagnostics.StackTrace", true, {
_ctor$0: function () {
Expand Down
4 changes: 1 addition & 3 deletions Libraries/JSIL.Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ function loadBinaryFileAsync (uri, onComplete) {
bytes = new VBArray(req.responseBody).toArray();
} else {
var text = req.responseText;
bytes = new Array(req.responseText.length);
for (var i = 0, l = text.length; i < l; i++)
bytes[i] = text.charCodeAt(i) & 0xFF;
bytes = JSIL.StringToByteArray(text);
}

onComplete(bytes, null);
Expand Down
27 changes: 27 additions & 0 deletions Libraries/JSIL.Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2885,4 +2885,31 @@ JSIL.MakeDelegate = function (fullName, isPublic, genericArguments) {
};

JSIL.RegisterName(fullName, assembly, isPublic, creator);
};

JSIL.StringToByteArray = function (text) {
var result = new Array(text.length);

for (var i = 0, l = text.length; i < l; i++)
result[i] = text.charCodeAt(i) & 0xFF;

return result;
};

JSIL.StringToCharArray = function (text) {
var result = new Array(text.length);

for (var i = 0, l = text.length; i < l; i++)
result[i] = text[i];

return result;
};

JSIL.CompareNumbers = function (lhs, rhs) {
if (lhs > rhs)
return 1;
else if (lhs < rhs)
return -1;
else
return 0;
};
60 changes: 33 additions & 27 deletions Proxies/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,67 @@ public abstract class StringProxy {
[JSExternal]
[JSRuntimeDispatch]
[JSIsPure]
new public static string Format (params AnyType[] arguments) {
public static string Format (params AnyType[] arguments) {
throw new InvalidOperationException();
}

[JSReplacement("JSIL.ConcatString.apply(null, $arguments)")]
[JSIsPure]
new public static string Concat (params string[] arguments) {
public static string Concat (params string[] arguments) {
throw new InvalidOperationException();
}

[JSReplacement("JSIL.ConcatString.apply(null, $arguments)")]
[JSIsPure]
new public static string Concat (params object[] arguments) {
public static string Concat (params object[] arguments) {
throw new InvalidOperationException();
}

[JSReplacement("String($value)")]
[JSIsPure]
new public static string Concat (object value) {
public static string Concat (object value) {
throw new InvalidOperationException();
}

[JSReplacement("JSIL.ConcatString($a, $b)")]
[JSIsPure]
new public static string Concat (object a, object b) {
public static string Concat (object a, object b) {
throw new InvalidOperationException();
}

[JSReplacement("($a + $b)")]
[JSIsPure]
new public static string Concat (string a, string b) {
public static string Concat (string a, string b) {
throw new InvalidOperationException();
}

[JSReplacement("JSIL.ConcatString($a, $b, $c)")]
[JSIsPure]
new public static string Concat (object a, object b, object c) {
public static string Concat (object a, object b, object c) {
throw new InvalidOperationException();
}

[JSReplacement("($a + $b + $c)")]
[JSIsPure]
new public static string Concat (string a, string b, string c) {
public static string Concat (string a, string b, string c) {
throw new InvalidOperationException();
}

[JSReplacement("JSIL.ConcatString($a, $b, $c, $d)")]
[JSIsPure]
new public static string Concat (object a, object b, object c, object d) {
public static string Concat (object a, object b, object c, object d) {
throw new InvalidOperationException();
}

[JSReplacement("($a + $b + $c + $d)")]
[JSIsPure]
new public static string Concat (string a, string b, string c, string d) {
public static string Concat (string a, string b, string c, string d) {
throw new InvalidOperationException();
}

[JSReplacement("$lhs + $ch")]
[JSIsPure]
new public static string Concat (string lhs, char ch) {
public static string Concat (string lhs, char ch) {
throw new InvalidOperationException();
}

Expand Down Expand Up @@ -112,100 +112,106 @@ public abstract class StringProxy {

[JSReplacement("$this.toLowerCase()")]
[JSIsPure]
new public string ToLower () {
public string ToLower () {
throw new InvalidOperationException();
}

[JSReplacement("$this.toUpperCase()")]
[JSIsPure]
new public string ToUpper () {
public string ToUpper () {
throw new InvalidOperationException();
}

[JSReplacement("System.String.StartsWith($this, $text)")]
[JSIsPure]
new public bool StartsWith (string text) {
public bool StartsWith (string text) {
throw new InvalidOperationException();
}

[JSReplacement("System.String.EndsWith($this, $text)")]
[JSIsPure]
new public bool EndsWith (string text) {
public bool EndsWith (string text) {
throw new InvalidOperationException();
}

[JSReplacement("$this.trim()")]
[JSIsPure]
new public string Trim () {
public string Trim () {
throw new InvalidOperationException();
}

[JSReplacement("System.String.Compare($this, $rhs)")]
[JSIsPure]
new public int CompareTo (string rhs) {
public int CompareTo (string rhs) {
throw new InvalidOperationException();
}

[JSReplacement("$this.indexOf($value)")]
[JSIsPure]
new public int IndexOf (char value) {
public int IndexOf (char value) {
throw new InvalidOperationException();
}

[JSReplacement("$this.indexOf($value, $startIndex)")]
[JSIsPure]
new public int IndexOf (char value, int startIndex) {
public int IndexOf (char value, int startIndex) {
throw new InvalidOperationException();
}

[JSReplacement("$this.indexOf($value)")]
[JSIsPure]
new public int IndexOf (string value) {
public int IndexOf (string value) {
throw new InvalidOperationException();
}

[JSReplacement("$this.indexOf($value, $startIndex)")]
[JSIsPure]
new public int IndexOf (string value, int startIndex) {
public int IndexOf (string value, int startIndex) {
throw new InvalidOperationException();
}

[JSReplacement("$this.lastIndexOf($value)")]
[JSIsPure]
new public int LastIndexOf (char value) {
public int LastIndexOf (char value) {
throw new InvalidOperationException();
}

[JSReplacement("$this.lastIndexOf($value)")]
[JSIsPure]
new public int LastIndexOf (string value) {
public int LastIndexOf (string value) {
throw new InvalidOperationException();
}

// FIXME
/*
[JSReplacement("$this.lastIndexOf($value, $startIndex)")]
[JSIsPure]
new public int LastIndexOf (char value, int startIndex) {
public int LastIndexOf (char value, int startIndex) {
throw new InvalidOperationException();
}
[JSReplacement("$this.lastIndexOf($value, $startIndex)")]
[JSIsPure]
new public int LastIndexOf (string value, int startIndex) {
public int LastIndexOf (string value, int startIndex) {
throw new InvalidOperationException();
}
*/

[JSReplacement("System.String.Replace($this, $oldText, $newText)")]
[JSIsPure]
new public string Replace (string oldText, string newText) {
public string Replace (string oldText, string newText) {
throw new InvalidOperationException();
}

[JSReplacement("System.String.Replace($this, $oldChar, $newChar)")]
[JSIsPure]
new public string Replace (char oldChar, char newChar) {
public string Replace (char oldChar, char newChar) {
throw new InvalidOperationException();
}

[JSReplacement("JSIL.StringToCharArray($this)")]
[JSIsPure]
public char[] ToCharArray () {
throw new InvalidOperationException();
}
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/SimpleTestCases/StringToCharArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

public static class Program {
public static void Main (string[] args) {
var arr = "ABCD".ToCharArray();

foreach (var ch in arr)
Console.WriteLine(ch);
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
<None Include="TestCases\EnumFieldAssignment.cs" />
<None Include="TestCases\CastToBoolean.cs" />
<None Include="TestCases\EnumNegation.cs" />
<None Include="SimpleTestCases\StringToCharArray.cs" />
<Compile Include="TestCases\StaticInitializersInGenericTypesSettingStaticFields.cs" />
<Compile Include="Util.cs" />
<Compile Include="VerbatimTests.cs" />
Expand Down

0 comments on commit 9af36b6

Please sign in to comment.