Ignore:
Timestamp:
Dec 2, 2013, 8:38:58 AM (11 years ago)
Author:
[email protected]
Message:

jsc: implement a native readFile function
https://bugs.webkit.org/show_bug.cgi?id=125059

Patch by Brian J. Burg <Brian Burg> on 2013-12-02
Reviewed by Filip Pizlo.

This adds a native readFile() function to jsc, used to slurp
an entire file into a JavaScript string.

  • jsc.cpp:

(GlobalObject::finishCreation): Add readFile() to globals.
(functionReadFile): Added.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/jsc.cpp

    r159064 r159934  
    109109static EncodedJSValue JSC_HOST_CALL functionRun(ExecState*);
    110110static EncodedJSValue JSC_HOST_CALL functionLoad(ExecState*);
     111static EncodedJSValue JSC_HOST_CALL functionReadFile(ExecState*);
    111112static EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState*);
    112113static EncodedJSValue JSC_HOST_CALL functionReadline(ExecState*);
     
    227228        addFunction(vm, "run", functionRun, 1);
    228229        addFunction(vm, "load", functionLoad, 1);
     230        addFunction(vm, "readFile", functionReadFile, 1);
    229231        addFunction(vm, "checkSyntax", functionCheckSyntax, 1);
    230232        addFunction(vm, "jscStack", functionJSCStack, 1);
     
    420422        exec->vm().throwException(exec, evaluationException);
    421423    return JSValue::encode(result);
     424}
     425
     426EncodedJSValue JSC_HOST_CALL functionReadFile(ExecState* exec)
     427{
     428    String fileName = exec->argument(0).toString(exec)->value(exec);
     429    Vector<char> script;
     430    if (!fillBufferWithContentsOfFile(fileName, script))
     431        return JSValue::encode(exec->vm().throwException(exec, createError(exec, "Could not open file.")));
     432
     433    return JSValue::encode(jsString(exec, stringFromUTF(script.data())));
    422434}
    423435
Note: See TracChangeset for help on using the changeset viewer.