Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pxtsim/domhost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Browser implementation of the pxsim.host environment.
namespace pxsim.host {
setEnvironment({
postToParent: data => {
// TODO: origins
if (typeof window !== 'undefined' && window.parent && window.parent.postMessage) {
window.parent.postMessage(data, "*");
}
},
locationHref: () => typeof window !== "undefined" ? window.location.href : undefined,
isPxtElectron: () => typeof window != "undefined" && !!(window as any).pxtElectron,
isIpcRenderer: () => typeof window != "undefined" && !!(window as any).ipcRenderer,
frameId: () => Embed.frameid
});
}
157 changes: 157 additions & 0 deletions pxtsim/host.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// The host is what the DOM-free simulator core needs from the place it runs: a way to message the
// editor, a few facts about the page, a clock, timers and random numbers. Each part has a default
// that works without a browser. In the browser, domhost.ts installs an environment that posts to
// the parent window.
namespace pxsim.host {
export interface Environment {
/** Delivers a message to the parent (editor) window, if any. */
postToParent(data: SimulatorMessage): void;
/** The current page URL, or undefined when there is no page. */
locationHref(): string | undefined;
isPxtElectron(): boolean;
isIpcRenderer(): boolean;
/** Id of the simulator frame hosting this runtime, if any. */
frameId(): string | undefined;
}

const defaultEnvironment: Environment = {
postToParent: () => { },
locationHref: () => undefined,
isPxtElectron: () => false,
isIpcRenderer: () => false,
frameId: () => undefined
};

let current: Environment = defaultEnvironment;

export function environment(): Environment {
return current;
}

/** Overrides parts of the environment. Anything left out goes back to its default. */
export function setEnvironment(env: Partial<Environment>) {
current = { ...defaultEnvironment, ...env };
}

/** The time source the runtime reads. */
export interface Clock {
/** Current time in milliseconds since an arbitrary epoch. */
now(): number;
/** Current time in microseconds, from the highest-resolution source available. */
perfNowUs(): number;
}

/** A pending timer or frame request. Pass it to the matching clear or cancel call. */
export type TimerHandle = unknown;

/** The timer source the runtime schedules work on. */
export interface Timers {
setTimeout(fn: () => void, ms: number): TimerHandle;
clearTimeout(id: TimerHandle): void;
setInterval(fn: () => void, ms: number): TimerHandle;
clearInterval(id: TimerHandle): void;
/** Runs fn before the next display frame. */
requestFrame(fn: () => void): TimerHandle;
cancelFrame(id: TimerHandle): void;
}

/** The random number source behind program randomness. */
export interface RandomSource {
/** Returns the next number in [0, 1). */
next(): number;
/** Seeds the generator. A source that cannot be seeded ignores this. */
seed(value: number): void;
}

/**
* Creates a random source that always gives the same sequence for the same seed. It uses the
* mulberry32 generator. Calling `seed` restarts the sequence from the new value.
*/
export function createSeededRandom(seed: number): RandomSource {
let state = seed;
return {
next: () => {
state = (state + 0x6D2B79F5) | 0;
let t = state;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
},
seed: value => { state = value; }
};
}

/**
* Returns performance.now, or a vendor-prefixed version of it. Falls back to Date.now when
* there is none.
*/
function highResolutionNow(): () => number {
if (typeof performance == "undefined")
return Date.now;
const p = performance as any;
const now: () => number = p.now || p.mozNow || p.msNow || p.webkitNow || p.oNow;
return now ? now.bind(performance) : Date.now;
}

let perf: () => number;

const defaultClock: Clock = {
now: () => Date.now(),
perfNowUs: () => {
if (!perf)
perf = highResolutionNow();
return perf() * 1000;
}
};

/**
* One frame at about 60 Hz, in milliseconds. requestFrame waits this long when there is no
* display frame to wait for.
*/
export const FRAME_MS = 16;

const defaultTimers: Timers = {
setTimeout: (fn, ms) => setTimeout(fn, ms),
clearTimeout: id => clearTimeout(id as any),
setInterval: (fn, ms) => setInterval(fn, ms),
clearInterval: id => clearInterval(id as any),
requestFrame: fn => typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame(fn) : setTimeout(fn, FRAME_MS),
cancelFrame: id => typeof cancelAnimationFrame !== "undefined" ? cancelAnimationFrame(id as any) : clearTimeout(id as any)
};

const defaultRng: RandomSource = {
next: () => Math.random(),
seed: () => { }
};

let currentClock: Clock = defaultClock;
let currentTimers: Timers = defaultTimers;
let currentRng: RandomSource = defaultRng;

export function clock(): Clock {
return currentClock;
}

/** Overrides parts of the clock. Anything left out goes back to its default. */
export function setClock(c: Partial<Clock>) {
currentClock = { ...defaultClock, ...c };
}

export function timers(): Timers {
return currentTimers;
}

/** Overrides parts of the timers. Anything left out goes back to its default. */
export function setTimers(t: Partial<Timers>) {
currentTimers = { ...defaultTimers, ...t };
}

export function rng(): RandomSource {
return currentRng;
}

/** Overrides parts of the random source. Anything left out goes back to its default. */
export function setRng(r: Partial<RandomSource>) {
currentRng = { ...defaultRng, ...r };
}
}
2 changes: 1 addition & 1 deletion pxtsim/langsupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace pxsim {

export namespace pxtcore {
export function seedAddRandom(num: number) {
// nothing yet
host.rng().seed(num);
}

export function mkAction(len: number, fn: LabelFn) {
Expand Down
6 changes: 3 additions & 3 deletions pxtsim/libgeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ namespace pxsim {
export function atanh(x: number) { return Math.atanh(x) }
export function atan2(y: number, x: number) { return Math.atan2(y, x) }
export function trunc(x: number) { return x > 0 ? Math.floor(x) : Math.ceil(x); }
export function random(): number { return Math.random(); }
export function random(): number { return host.rng().next(); }
export function randomRange(min: number, max: number): number {
if (min == max) return min;
if (min > max) {
Expand All @@ -249,9 +249,9 @@ namespace pxsim {
max = t;
}
if (Math.floor(min) == min && Math.floor(max) == max)
return min + Math.floor(Math.random() * (max - min + 1));
return min + Math.floor(host.rng().next() * (max - min + 1));
else
return min + Math.random() * (max - min);
return min + host.rng().next() * (max - min);
}
}

Expand Down
Loading
Loading