Skip to content

Commit a049bee

Browse files
authored
Internalize JS functions. NFC (#18988)
This came out of my work on #18979. Without the $ prefix on these symbols they are effectively exposed to native code and my code tried to figure out their native signatures, which can't exist because these functions are not called from native code and mostly take JS object as arguments.
1 parent 36eb862 commit a049bee

8 files changed

+131
-131
lines changed

src/library.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ mergeInto(LibraryManager.library, {
445445
// time.h
446446
// ==========================================================================
447447

448-
_mktime_js__deps: ['_yday_from_date'],
448+
_mktime_js__deps: ['$ydayFromDate'],
449449
_mktime_js__sig: 'ip',
450450
_mktime_js: function(tmPtr) {
451451
var date = new Date({{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_year, 'i32') }}} + 1900,
@@ -476,7 +476,7 @@ mergeInto(LibraryManager.library, {
476476
}
477477

478478
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_wday, 'date.getDay()', 'i32') }}};
479-
var yday = __yday_from_date(date)|0;
479+
var yday = ydayFromDate(date)|0;
480480
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_yday, 'yday', 'i32') }}};
481481
// To match expected behavior, update fields from date
482482
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'date.getSeconds()', 'i32') }}};
@@ -524,7 +524,7 @@ mergeInto(LibraryManager.library, {
524524
return (date.getTime() / 1000)|0;
525525
},
526526

527-
_localtime_js__deps: ['$readI53FromI64', '_yday_from_date'],
527+
_localtime_js__deps: ['$readI53FromI64', '$ydayFromDate'],
528528
_localtime_js__sig: 'ipp',
529529
_localtime_js: function(time, tmPtr) {
530530
var date = new Date({{{ makeGetValue('time', 0, 'i53') }}}*1000);
@@ -536,7 +536,7 @@ mergeInto(LibraryManager.library, {
536536
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_year, 'date.getFullYear()-1900', 'i32') }}};
537537
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_wday, 'date.getDay()', 'i32') }}};
538538

539-
var yday = __yday_from_date(date)|0;
539+
var yday = ydayFromDate(date)|0;
540540
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_yday, 'yday', 'i32') }}};
541541
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_gmtoff, '-(date.getTimezoneOffset() * 60)', 'i32') }}};
542542

@@ -631,39 +631,39 @@ mergeInto(LibraryManager.library, {
631631
}
632632
},
633633

634-
_MONTH_DAYS_REGULAR: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
635-
_MONTH_DAYS_LEAP: [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
636-
_MONTH_DAYS_REGULAR_CUMULATIVE: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
637-
_MONTH_DAYS_LEAP_CUMULATIVE: [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335],
634+
$MONTH_DAYS_REGULAR: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
635+
$MONTH_DAYS_LEAP: [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
636+
$MONTH_DAYS_REGULAR_CUMULATIVE: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
637+
$MONTH_DAYS_LEAP_CUMULATIVE: [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335],
638638

639-
_isLeapYear: function(year) {
639+
$isLeapYear: function(year) {
640640
return year%4 === 0 && (year%100 !== 0 || year%400 === 0);
641641
},
642642

643-
_yday_from_date__deps: ['_isLeapYear', '_MONTH_DAYS_LEAP_CUMULATIVE', '_MONTH_DAYS_REGULAR_CUMULATIVE'],
644-
_yday_from_date: function(date) {
645-
var isLeapYear = __isLeapYear(date.getFullYear());
646-
var monthDaysCumulative = (isLeapYear ? __MONTH_DAYS_LEAP_CUMULATIVE : __MONTH_DAYS_REGULAR_CUMULATIVE);
643+
$ydayFromDate__deps: ['$isLeapYear', '$MONTH_DAYS_LEAP_CUMULATIVE', '$MONTH_DAYS_REGULAR_CUMULATIVE'],
644+
$ydayFromDate: function(date) {
645+
var leap = isLeapYear(date.getFullYear());
646+
var monthDaysCumulative = (leap ? MONTH_DAYS_LEAP_CUMULATIVE : MONTH_DAYS_REGULAR_CUMULATIVE);
647647
var yday = monthDaysCumulative[date.getMonth()] + date.getDate() - 1; // -1 since it's days since Jan 1
648648

649649
return yday;
650650
},
651651

652-
_arraySum: function(array, index) {
652+
$arraySum: function(array, index) {
653653
var sum = 0;
654654
for (var i = 0; i <= index; sum += array[i++]) {
655655
// no-op
656656
}
657657
return sum;
658658
},
659659

660-
_addDays__deps: ['_isLeapYear', '_MONTH_DAYS_LEAP', '_MONTH_DAYS_REGULAR'],
661-
_addDays: function(date, days) {
660+
$addDays__deps: ['$isLeapYear', '$MONTH_DAYS_LEAP', '$MONTH_DAYS_REGULAR'],
661+
$addDays: function(date, days) {
662662
var newDate = new Date(date.getTime());
663663
while (days > 0) {
664-
var leap = __isLeapYear(newDate.getFullYear());
664+
var leap = isLeapYear(newDate.getFullYear());
665665
var currentMonth = newDate.getMonth();
666-
var daysInCurrentMonth = (leap ? __MONTH_DAYS_LEAP : __MONTH_DAYS_REGULAR)[currentMonth];
666+
var daysInCurrentMonth = (leap ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR)[currentMonth];
667667

668668
if (days > daysInCurrentMonth-newDate.getDate()) {
669669
// we spill over to next month
@@ -687,7 +687,7 @@ mergeInto(LibraryManager.library, {
687687

688688
// Note: this is not used in STANDALONE_WASM mode, because it is more
689689
// compact to do it in JS.
690-
strftime__deps: ['_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP',
690+
strftime__deps: ['$isLeapYear', '$arraySum', '$addDays', '$MONTH_DAYS_REGULAR', '$MONTH_DAYS_LEAP',
691691
'$intArrayFromString', '$writeArrayToMemory'
692692
],
693693
strftime__sig: 'ppppp',
@@ -798,7 +798,7 @@ mergeInto(LibraryManager.library, {
798798
}
799799

800800
function getWeekBasedYear(date) {
801-
var thisDate = __addDays(new Date(date.tm_year+1900, 0, 1), date.tm_yday);
801+
var thisDate = addDays(new Date(date.tm_year+1900, 0, 1), date.tm_yday);
802802

803803
var janFourthThisYear = new Date(thisDate.getFullYear(), 0, 4);
804804
var janFourthNextYear = new Date(thisDate.getFullYear()+1, 0, 4);
@@ -866,7 +866,7 @@ mergeInto(LibraryManager.library, {
866866
},
867867
'%j': function(date) {
868868
// Day of the year (001-366)
869-
return leadingNulls(date.tm_mday+__arraySum(__isLeapYear(date.tm_year+1900) ? __MONTH_DAYS_LEAP : __MONTH_DAYS_REGULAR, date.tm_mon-1), 3);
869+
return leadingNulls(date.tm_mday + arraySum(isLeapYear(date.tm_year+1900) ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, date.tm_mon-1), 3);
870870
},
871871
'%m': function(date) {
872872
return leadingNulls(date.tm_mon+1, 2);
@@ -913,14 +913,14 @@ mergeInto(LibraryManager.library, {
913913
// If 31 December of prev year a Thursday, or Friday of a
914914
// leap year, then the prev year has 53 weeks.
915915
var dec31 = (date.tm_wday + 7 - date.tm_yday - 1) % 7;
916-
if (dec31 == 4 || (dec31 == 5 && __isLeapYear(date.tm_year%400-1))) {
916+
if (dec31 == 4 || (dec31 == 5 && isLeapYear(date.tm_year%400-1))) {
917917
val++;
918918
}
919919
} else if (val == 53) {
920920
// If 1 January is not a Thursday, and not a Wednesday of a
921921
// leap year, then this year has only 52 weeks.
922922
var jan1 = (date.tm_wday + 371 - date.tm_yday) % 7;
923-
if (jan1 != 4 && (jan1 != 3 || !__isLeapYear(date.tm_year)))
923+
if (jan1 != 4 && (jan1 != 3 || !isLeapYear(date.tm_year)))
924924
val = 1;
925925
}
926926
return leadingNulls(val, 2);
@@ -982,7 +982,7 @@ mergeInto(LibraryManager.library, {
982982
return _strftime(s, maxsize, format, tm); // no locale support yet
983983
},
984984

985-
strptime__deps: ['_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP',
985+
strptime__deps: ['$isLeapYear', '$arraySum', '$addDays', '$MONTH_DAYS_REGULAR', '$MONTH_DAYS_LEAP',
986986
'$jstoi_q', '$intArrayFromString' ],
987987
strptime__sig: 'pppp',
988988
strptime: function(buf, format, tm) {
@@ -1143,10 +1143,10 @@ mergeInto(LibraryManager.library, {
11431143
} else if ((value=getMatch('j'))) {
11441144
// get day of month from day of year ...
11451145
var day = jstoi_q(value);
1146-
var leapYear = __isLeapYear(date.year);
1146+
var leapYear = isLeapYear(date.year);
11471147
for (var month=0; month<12; ++month) {
1148-
var daysUntilMonth = __arraySum(leapYear ? __MONTH_DAYS_LEAP : __MONTH_DAYS_REGULAR, month-1);
1149-
if (day<=daysUntilMonth+(leapYear ? __MONTH_DAYS_LEAP : __MONTH_DAYS_REGULAR)[month]) {
1148+
var daysUntilMonth = arraySum(leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, month-1);
1149+
if (day<=daysUntilMonth+(leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR)[month]) {
11501150
date.day = day-daysUntilMonth;
11511151
}
11521152
}
@@ -1165,10 +1165,10 @@ mergeInto(LibraryManager.library, {
11651165
var endDate;
11661166
if (janFirst.getDay() === 0) {
11671167
// Jan 1st is a Sunday, and, hence in the 1st CW
1168-
endDate = __addDays(janFirst, weekDayNumber+7*(weekNumber-1));
1168+
endDate = addDays(janFirst, weekDayNumber+7*(weekNumber-1));
11691169
} else {
11701170
// Jan 1st is not a Sunday, and, hence still in the 0th CW
1171-
endDate = __addDays(janFirst, 7-janFirst.getDay()+weekDayNumber+7*(weekNumber-1));
1171+
endDate = addDays(janFirst, 7-janFirst.getDay()+weekDayNumber+7*(weekNumber-1));
11721172
}
11731173
date.day = endDate.getDate();
11741174
date.month = endDate.getMonth();
@@ -1184,10 +1184,10 @@ mergeInto(LibraryManager.library, {
11841184
var endDate;
11851185
if (janFirst.getDay()===1) {
11861186
// Jan 1st is a Monday, and, hence in the 1st CW
1187-
endDate = __addDays(janFirst, weekDayNumber+7*(weekNumber-1));
1187+
endDate = addDays(janFirst, weekDayNumber+7*(weekNumber-1));
11881188
} else {
11891189
// Jan 1st is not a Monday, and, hence still in the 0th CW
1190-
endDate = __addDays(janFirst, 7-janFirst.getDay()+1+weekDayNumber+7*(weekNumber-1));
1190+
endDate = addDays(janFirst, 7-janFirst.getDay()+1+weekDayNumber+7*(weekNumber-1));
11911191
}
11921192

11931193
date.day = endDate.getDate();
@@ -1215,7 +1215,7 @@ mergeInto(LibraryManager.library, {
12151215
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_mon, 'fullDate.getMonth()', 'i32') }}};
12161216
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_year, 'fullDate.getFullYear()-1900', 'i32') }}};
12171217
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_wday, 'fullDate.getDay()', 'i32') }}};
1218-
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_yday, '__arraySum(__isLeapYear(fullDate.getFullYear()) ? __MONTH_DAYS_LEAP : __MONTH_DAYS_REGULAR, fullDate.getMonth()-1)+fullDate.getDate()-1', 'i32') }}};
1218+
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_yday, 'arraySum(isLeapYear(fullDate.getFullYear()) ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, fullDate.getMonth()-1)+fullDate.getDate()-1', 'i32') }}};
12191219
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_isdst, '0', 'i32') }}};
12201220

12211221
// we need to convert the matched sequence into an integer array to take care of UTF-8 characters > 0x7F
@@ -2475,9 +2475,9 @@ mergeInto(LibraryManager.library, {
24752475
return [args, funcname, str];
24762476
},
24772477

2478-
emscripten_get_callstack_js__deps: ['$traverseStack', '$jsStackTrace', '$warnOnce'],
2479-
emscripten_get_callstack_js__docs: '/** @param {number=} flags */',
2480-
emscripten_get_callstack_js: function(flags) {
2478+
$getCallstack__deps: ['$traverseStack', '$jsStackTrace', '$warnOnce'],
2479+
$getCallstack__docs: '/** @param {number=} flags */',
2480+
$getCallstack: function(flags) {
24812481
var callstack = jsStackTrace();
24822482

24832483
// Find the symbols in the callstack that corresponds to the functions that
@@ -2588,13 +2588,13 @@ mergeInto(LibraryManager.library, {
25882588
return callstack;
25892589
},
25902590

2591-
emscripten_get_callstack__deps: ['emscripten_get_callstack_js'],
2591+
emscripten_get_callstack__deps: ['$getCallstack'],
25922592
emscripten_get_callstack: function(flags, str, maxbytes) {
25932593
// Use explicit calls to from64 rather then using the __sig
25942594
// magic here. This is because the __sig wrapper uses arrow function
25952595
// notation which causes the inner call to traverseStack to fail.
25962596
{{{ from64('str') }}};
2597-
var callstack = _emscripten_get_callstack_js(flags);
2597+
var callstack = getCallstack(flags);
25982598
// User can query the required amount of bytes to hold the callstack.
25992599
if (!str || maxbytes <= 0) {
26002600
return lengthBytesUTF8(callstack)+1;
@@ -2606,11 +2606,11 @@ mergeInto(LibraryManager.library, {
26062606
return bytesWrittenExcludingNull+1;
26072607
},
26082608

2609-
emscripten_log_js__deps: ['emscripten_get_callstack_js'],
2610-
emscripten_log_js: function(flags, str) {
2609+
$emscriptenLog__deps: ['$getCallstack'],
2610+
$emscriptenLog: function(flags, str) {
26112611
if (flags & {{{ cDefine('EM_LOG_C_STACK') | cDefine('EM_LOG_JS_STACK') }}}) {
26122612
str = str.replace(/\s+$/, ''); // Ensure the message and the callstack are joined cleanly with exactly one newline.
2613-
str += (str.length > 0 ? '\n' : '') + _emscripten_get_callstack_js(flags);
2613+
str += (str.length > 0 ? '\n' : '') + getCallstack(flags);
26142614
}
26152615

26162616
if (flags & {{{ cDefine('EM_LOG_CONSOLE') }}}) {
@@ -2633,11 +2633,11 @@ mergeInto(LibraryManager.library, {
26332633
},
26342634

26352635
emscripten_log__sig: 'vipp',
2636-
emscripten_log__deps: ['$formatString', 'emscripten_log_js'],
2636+
emscripten_log__deps: ['$formatString', '$emscriptenLog'],
26372637
emscripten_log: function(flags, format, varargs) {
26382638
var result = formatString(format, varargs);
26392639
var str = UTF8ArrayToString(result, 0);
2640-
_emscripten_log_js(flags, str);
2640+
emscriptenLog(flags, str);
26412641
},
26422642

26432643
// We never free the return values of this function so we need to allocate
@@ -3121,9 +3121,9 @@ mergeInto(LibraryManager.library, {
31213121
#if LINK_AS_CXX
31223122
// libunwind
31233123

3124-
_Unwind_Backtrace__deps: ['emscripten_get_callstack_js'],
3124+
_Unwind_Backtrace__deps: ['$getCallstack'],
31253125
_Unwind_Backtrace: function(func, arg) {
3126-
var trace = _emscripten_get_callstack_js();
3126+
var trace = getCallstack();
31273127
var parts = trace.split('\n');
31283128
for (var i = 0; i < parts.length; i++) {
31293129
var ret = {{{ makeDynCall('iii', 'func') }}}(0, arg);

0 commit comments

Comments
 (0)