type PhpNullish = null | undefined
type PhpInput = {} | PhpNullish
type PhpScalar = string | number | boolean
type PhpPrimitive = PhpScalar | bigint
type PhpList<T = PhpInput> = T[]
type PhpAssoc<T = PhpInput> = { [key: string]: T }
type PhpFunctionValue = (...args: PhpInput[]) => PhpInput
interface PhpRuntimeAssoc { [key: string]: PhpRuntimeValue }
interface PhpRuntimeList extends Array<PhpRuntimeValue> {}
type PhpRuntimeValue = PhpPrimitive | PhpNullish | PhpRuntimeList | PhpRuntimeAssoc | PhpFunctionValue
interface IniEntry { local_value?: PhpInput }
type LocaleEntry = PhpAssoc<PhpInput> & { sorting?: (left: PhpInput, right: PhpInput) => number }
type LocaleCategoryMap = PhpAssoc<string | undefined>
interface LocutusRuntimeContainer { php?: PhpAssoc<PhpInput> }
interface PhpRuntimeKnownEntryMap { ini: PhpAssoc<IniEntry | undefined> locales: PhpAssoc<LocaleEntry | undefined> localeCategories: LocaleCategoryMap pointers: PhpList<PhpInput> locale_default: string locale: string uniqidSeed: number timeoutStatus: boolean last_error_json: number strtokleftOver: string }
type GlobalWithLocutus = { $locutus?: LocutusRuntimeContainer [key: string]: PhpInput }
const globalContext: GlobalWithLocutus = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {}
const ensurePhpRuntimeObject = (): PhpAssoc<PhpInput> => { let locutus = globalContext.$locutus if (typeof locutus !== 'object' || locutus === null) { locutus = {} globalContext.$locutus = locutus }
let php = locutus.php if (typeof php !== 'object' || php === null) { php = {} locutus.php = php }
return php }
function setPhpRuntimeEntry<TKey extends keyof PhpRuntimeKnownEntryMap>( key: TKey, value: PhpRuntimeKnownEntryMap[TKey], ): void
function setPhpRuntimeEntry(key: string, value: PhpInput): void
function setPhpRuntimeEntry(key: string, value: PhpInput): void { const php = ensurePhpRuntimeObject() php[key] = value }
type JsonPrimitive = string | number | boolean | null
type JsonObject = { [key: string]: JsonValue }
type JsonValue = JsonPrimitive | JsonValue[] | JsonObject
type JsonEncodeInput = PhpRuntimeValue
const isJsonObject = (value: PhpRuntimeValue): value is JsonObject => typeof value === 'object' && value !== null && !Array.isArray(value)
function json_encode(mixedVal: JsonEncodeInput): string | null {
const json = typeof JSON === 'object' && JSON !== null ? JSON : null let retVal try { if (typeof json === 'object' && json !== null) { const stringify = json.stringify if (typeof stringify === 'function') { retVal = stringify.call(json, mixedVal) if (retVal === undefined) { throw new SyntaxError('json_encode') } return retVal } }
const value = mixedVal
const quote = function (string: string): string { const escapeChars = [ '\u0000-\u001f', '\u007f-\u009f', '\u00ad', '\u0600-\u0604', '\u070f', '\u17b4', '\u17b5', '\u200c-\u200f', '\u2028-\u202f', '\u2060-\u206f', '\ufeff', '\ufff0-\uffff', ].join('') const escapable = new RegExp('[\\"' + escapeChars + ']', 'g') const meta: Record<string, string> = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\', }
escapable.lastIndex = 0 return escapable.test(string) ? '"' + string.replace(escapable, function (a) { const c = meta[a] return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4) }) + '"' : '"' + string + '"' }
const _str = function ( key: string | number, holder: PhpAssoc<PhpRuntimeValue> | PhpRuntimeValue[], ): string | undefined { let gap = '' const indent = ' ' let i = 0 let k = '' let v = '' let length = 0 const mind = gap let partial: string[] = [] let value = Array.isArray(holder) ? holder[Number(key)] : holder[String(key)]
if (typeof value === 'object' && value !== null) { if ('toJSON' in value && typeof value.toJSON === 'function') { value = value.toJSON.call(value, key) } }
switch (typeof value) { case 'string': return quote(value)
case 'number': return isFinite(value) ? String(value) : 'null'
case 'boolean': return String(value)
case 'object': if (!value) { return 'null' }
gap += indent partial = []
if (Array.isArray(value)) { length = value.length for (i = 0; i < length; i += 1) { partial[i] = _str(i, value) || 'null' }
v = partial.length === 0 ? '[]' : gap ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : '[' + partial.join(',') + ']' return v }
if (!isJsonObject(value)) { throw new SyntaxError('json_encode') } for (const [entryKey] of Object.entries(value)) { k = entryKey v = _str(k, value) || '' if (v) { partial.push(quote(k) + (gap ? ': ' : ':') + v) } }
v = partial.length === 0 ? '{}' : gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : '{' + partial.join(',') + '}' return v case 'undefined': case 'function': default: throw new SyntaxError('json_encode') } }
const encoded = _str('', { '': value, }) if (typeof encoded !== 'string') { throw new SyntaxError('json_encode') } return encoded } catch (err) { if (!(err instanceof SyntaxError)) { throw new Error('Unexpected error type in json_encode()') } setPhpRuntimeEntry('last_error_json', 4) return null } }
|