Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MB-17567 Add startup snapshot for builtin JavaScript functions
Change-Id: I250172a9414bc75e818bc852c87032c93f602dc0 Reviewed-on: http://review.couchbase.org/62053 Reviewed-by: Dave Rigby <daver@couchbase.com> Tested-by: buildbot <build@couchbase.com> Reviewed-by: Volker Mische <volker.mische@gmail.com>
- Loading branch information
Showing
6 changed files
with
159 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
SET(BUILTIN_JS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/builtin.js) | ||
|
||
TRY_RUN(EMBED_DATA_EXITCODE EMBED_DATA_COMPILED | ||
${CMAKE_CURRENT_BINARY_DIR} | ||
${CMAKE_CURRENT_SOURCE_DIR}/embed_data.c | ||
RUN_OUTPUT_VARIABLE JSFUNCTION_CONTENTS | ||
ARGS ${BUILTIN_JS_FILE}) | ||
|
||
STRING(REPLACE "\r" "\n" JSFUNCTION_CONTENTS "${JSFUNCTION_CONTENTS}") | ||
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/jsfunctions_data.cc "${JSFUNCTION_CONTENTS}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @copyright 2016 Couchbase, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
**/ | ||
|
||
function sum(values) { | ||
var sum = 0; | ||
for (var i = 0; i < values.length; ++i) { | ||
sum += values[i]; | ||
} | ||
return sum; | ||
}; | ||
|
||
// I wish it was on the prototype, but that will require bigger | ||
// C changes as adding to the date prototype should be done on | ||
// process launch. The code you see here may be faster, but it | ||
// is less JavaScripty. | ||
// "Date.prototype.toArray = (function() {" | ||
function dateToArray(date) { | ||
date = date.getUTCDate ? date : new Date(date); | ||
return isFinite(date.valueOf()) ? | ||
[date.getUTCFullYear(), | ||
(date.getUTCMonth() + 1), | ||
date.getUTCDate(), | ||
date.getUTCHours(), | ||
date.getUTCMinutes(), | ||
date.getUTCSeconds()] : null; | ||
}; | ||
|
||
function decodeBase64(b64) { | ||
var i, j, l, tmp, scratch, arr = []; | ||
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
if (typeof b64 !== 'string') { | ||
throw 'Input is not a string'; | ||
} | ||
if (b64.length % 4 > 0) { | ||
throw 'Invalid base64 source.'; | ||
} | ||
scratch = b64.indexOf('='); | ||
scratch = scratch > 0 ? b64.length - scratch : 0; | ||
l = scratch > 0 ? b64.length - 4 : b64.length; | ||
for (i = 0, j = 0; i < l; i += 4, j += 3) { | ||
tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12); | ||
tmp |= (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]); | ||
arr.push((tmp & 0xFF0000) >> 16); | ||
arr.push((tmp & 0xFF00) >> 8); | ||
arr.push(tmp & 0xFF); | ||
} | ||
if (scratch === 2) { | ||
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4); | ||
arr.push(tmp & 0xFF); | ||
} else if (scratch === 1) { | ||
tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4); | ||
tmp |= (lookup.indexOf(b64[i + 2]) >> 2); | ||
arr.push((tmp >> 8) & 0xFF); | ||
arr.push(tmp & 0xFF); | ||
} | ||
return arr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @copyright 2016 Couchbase, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
**/ | ||
|
||
/* This code takes in list of file names in argv and embed the contents | ||
* into a const char array. | ||
**/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
FILE *fp; | ||
int i, j, ch; | ||
|
||
printf("extern const unsigned char jsFunction_src[] = {"); | ||
for(i = 1; i < argc; i++) { | ||
if ((fp = fopen(argv[i], "rb")) == NULL) { | ||
exit(EXIT_FAILURE); | ||
} | ||
else { | ||
for (j = 0; (ch = fgetc(fp)) != EOF; j++) { | ||
if ((j % 12) == 0) { | ||
printf("%c", '\n'); | ||
} | ||
printf(" %#02x,", ch); | ||
} | ||
fclose(fp); | ||
} | ||
} | ||
|
||
// Append zero byte at the end, to make text files appear in memory | ||
// as nul-terminated strings. | ||
printf("%s", " 0x00\n};\n"); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @copyright 2016 Couchbase, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
**/ | ||
|
||
#ifndef _JSFUNCTION_DATA_H | ||
#define _JSFUNCTION_DATA_H | ||
|
||
extern const unsigned char jsFunction_src[]; | ||
|
||
#endif //_JSFUNCTION_DATA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters