Remove remaining external shims

This commit is contained in:
Michal Moskal 2016-04-01 22:46:06 -07:00
parent 6559f386d2
commit feb17c5e45
3 changed files with 101 additions and 31 deletions

View File

@ -78,6 +78,11 @@ namespace String {
{
return ManagedString((char)code).leakData();
}
//%
int toNumber(StringData *s) {
return atoi(s->data);
}
}
namespace NumberMethods {
@ -137,3 +142,92 @@ namespace Math {
return ::sqrt(x);
}
}
namespace ArrayImpl {
//%
RefCollection *mk(uint32_t flags)
{
RefCollection *r = new RefCollection(flags);
return r;
}
//%
int length(RefCollection *c) { return c->data.size(); }
//%
void push(RefCollection *c, uint32_t x) {
if (c->flags & 1) incr(x);
c->data.push_back(x);
}
inline bool in_range(RefCollection *c, int x) {
return (0 <= x && x < (int)c->data.size());
}
//%
uint32_t getAt(RefCollection *c, int x) {
if (in_range(c, x)) {
uint32_t tmp = c->data.at(x);
if (c->flags & 1) incr(tmp);
return tmp;
}
else {
error(ERR_OUT_OF_BOUNDS);
return 0;
}
}
//%
void removeAt(RefCollection *c, int x) {
if (!in_range(c, x))
return;
if (c->flags & 1) decr(c->data.at(x));
c->data.erase(c->data.begin()+x);
}
//%
void setAt(RefCollection *c, int x, uint32_t y) {
if (!in_range(c, x))
return;
if (c->flags & 1) {
decr(c->data.at(x));
incr(y);
}
c->data.at(x) = y;
}
//%
int indexOf(RefCollection *c, uint32_t x, int start) {
if (!in_range(c, start))
return -1;
if (c->flags & 2) {
StringData *xx = (StringData*)x;
for (uint32_t i = start; i < c->data.size(); ++i) {
StringData *ee = (StringData*)c->data.at(i);
if (xx->len == ee->len && memcmp(xx->data, ee->data, xx->len) == 0)
return (int)i;
}
} else {
for (uint32_t i = start; i < c->data.size(); ++i)
if (c->data.at(i) == x)
return (int)i;
}
return -1;
}
//%
int removeElement(RefCollection *c, uint32_t x) {
int idx = indexOf(c, x, 0);
if (idx >= 0) {
removeAt(c, idx);
return 1;
}
return 0;
}
}

View File

@ -4,14 +4,14 @@ interface Array<T> {
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
//% shim=collection::count
//% shim=ArrayImpl::length
length: number;
/**
* Appends new elements to an array.
* @param items New elements of the Array.
*/
//% shim=collection::add
//% shim=ArrayImpl::push
push(item: T): void;
/**
@ -41,11 +41,11 @@ interface Array<T> {
slice(start: number, end: number): T[];
/** Removes the first occurence of an object. Returns true if removed. */
//% shim=collection::remove
//% shim=ArrayImpl::removeElement
removeElement(element:T) : boolean;
/** Removes the object at position index. */
//% shim=collection::remove_at
//% shim=ArrayImpl::removeAt
removeAt(idx:number) : void;
@ -69,7 +69,7 @@ interface Array<T> {
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
*/
//% shim=collection::index_of
//% shim=ArrayImpl::indexOf
indexOf(searchElement: T, fromIndex?: number): number;
@ -86,7 +86,7 @@ interface String {
* Converts A string to an integer.
* @param s A string to convert into a number.
*/
//% shim=string::to_number
//% shim=String::toNumber
declare function parseInt(s: string): number;
interface Object {}
@ -94,27 +94,3 @@ interface Function {}
interface IArguments {}
interface RegExp {}
interface Boolean {
/**
* Returns a string representation of an object.
*/
//% shim=boolean::to_string
toString(): string;
}
declare namespace String {
/**
* Make a string from the given ASCII character code.
*/
//% shim=number::to_character
export function fromCharCode(code:number): string;
}
interface Number {
/**
* Returns a string representation of an object.
*/
//% shim=number::to_string
toString(): string;
}

View File

@ -32,7 +32,7 @@ declare interface String {
/** Returns the length of a String object. */
//% property shim=StringMethods::length
length(): number;
length: number;
}