Hi guys!
I was working on a very basic implementation of compiled cache for js.
I have edited the jsapi.cpp.
I get the following result:
*the first time I open a website it works perfectly.
*the second time: some websites work, other don't and make Firefox close.
(google.com works fine, gmail breaks, ). I'm wondering why such thing happens.
*the results I got in the log files show that there is a small improvement which is very fine for me because js raw scripts are read from the browser cache even if a compiled version exist( it was very complicated for me to remove that), but of course in this case we don't need to read them and we will gain the reading time.
Could someone please check the changes I made, and try it on his computer?
Could you tell me please the cause of the crash?
This are the changes I made.(jsapi.cpp)
size_t hash(char const *input) {
//stupid hash function to give a name to the js compiled file
const int ret_size = 32;
size_t ret = 0x555555;
const int per_char = 7;
while (*input) {
ret ^= *input++;
ret = ((ret << per_char) | (ret >> (ret_size - per_char)));
}
return ret;
}
uint32_t load_file_to_memory(const char *filename, char **result)
{
//stupid function to load file to memery and return its size
uint32_t size = 0;
FILE *f = fopen(filename, "rb");
if (f == NULL)
{
*result = NULL;
return -1; // -1 means file opening fail
}
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
*result = (char *)malloc(size+1);
if (size != fread(*result, sizeof(char), size, f))
{
free(*result);
return -2; // -2 means file reading fail
}
fclose(f);
(*result)[size] = 0;
return size;
}
//important staff here
extern JS_PUBLIC_API(bool)
JS::Evaluate(JSContext *cx, HandleObject obj, CompileOptions options,
const jschar *chars, size_t length, jsval *rval)
{
FILE * info;
JS_ToggleOptions(cx, JSOPTION_COMPILE_N_GO);
int64_t fdtime;
info = fopen ("/home/riadh/info.log","a");
void * jsfile;
uint32_t size;
char * buffer;
FILE *saveFile;
JSScript * src;
Maybe<AutoVersionAPI> mava;
if (options.versionSet) {
mava.construct(cx, options.version);
// AutoVersionAPI propagates some compilation flags through.
options.version = mava.ref().version();
}
JS_THREADSAFE_ASSERT(cx->compartment != cx->runtime->atomsCompartment);
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, obj);
JS_ASSERT_IF(options.principals, cx->compartment->principals == options.principals);
AutoLastFrameCheck lfc(cx);
options.setNoScriptRval(!rval);
compile = PRMJ_Now();
//disable compile and go
JS_SetOptions(cx, JS_GetOptions(cx) & ~JSOPTION_COMPILE_N_GO);
options.setCompileAndGo(false);
//don't save source, I tried with the source but I get the same bug
options.setSourcePolicy(CompileOptions::NO_SOURCE);
size_t ha = hash((char * )chars);
char str [200];
sprintf (str, "/home/riadh/tmp/js/%u.%u", ha, length);
fdtime = PRMJ_Now();//reading and deencoding time starts here
size = load_file_to_memory(str, &buffer);
JSScript * scr;
if ((buffer!=NULL) && (length>500) && (size>0))
{
scr = JS_DecodeScript(cx,(void *) buffer, size, 0, 0);
fdtime = PRMJ_Now() - fdtime;//reading and dencoding time ends here
fprintf(info, "\n\n%u opendencode %.3f ms slength %u csize %u\n\n",ha,
double(fdtime) / PRMJ_USEC_PER_MSEC,
length,size);
} else {
fdtime = PRMJ_Now();//compiling time starts here
scr = frontend::CompileScript(cx, obj, NULL, options, chars, length);
fdtime = PRMJ_Now() - fdtime;//compiling time ends here
if (length>500) {
jsfile = JS_EncodeScript(cx, scr, &size);//this is not counted as compiling time because this could be launch in a parallel thread after a while (after the end of all scripts execution for the current page)
fprintf(info, "\n\n%u compile %.3f ms slength %u csize %u\n\n",ha,
double(fdtime) / PRMJ_USEC_PER_MSEC,
length,size);
saveFile = fopen (str,"w");
int i;
char * c;
for(c=(char *) jsfile; c - (char *) jsfile <size;c++){
fputc ( *c ,saveFile );
}
fclose (saveFile);
}
}
fclose (info);
RootedScript script(cx, scr);
if (!script)
return false;
JS_PUBLIC_API(bool) tmp = Execute(cx, script, *obj, rval);
JS_ASSERT(script->getVersion() == options.version);
return tmp;
}
Best
Riadh