Home | History | Annotate | Download | only in lua
      1 /*
      2  * Copyright 2013 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkLua.h"
      9 #include "SkGraphics.h"
     10 #include "SkStream.h"
     11 #include "SkData.h"
     12 #include "SkOSFile.h"
     13 
     14 extern "C" {
     15     #include "lua.h"
     16     #include "lualib.h"
     17     #include "lauxlib.h"
     18 }
     19 
     20 static SkData* read_into_data(const char file[]) {
     21     SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
     22     if (!stream.get()) {
     23         return SkData::NewEmpty();
     24     }
     25     size_t len = stream->getLength();
     26     void* buffer = sk_malloc_throw(len);
     27     stream->read(buffer, len);
     28     return SkData::NewFromMalloc(buffer, len);
     29 }
     30 
     31 int tool_main(int argc, char** argv);
     32 int tool_main(int argc, char** argv) {
     33     SkAutoGraphics ag;
     34     SkLua L;
     35 
     36     for (int i = 1; i < argc; ++i) {
     37         SkData* data = NULL;
     38         const void* ptr;
     39         size_t len;
     40 
     41         if (!strcmp(argv[i], "--lua") && i < argc-1) {
     42             ptr = argv[i + 1];
     43             len = strlen(argv[i + 1]);
     44             i += 1;
     45         } else {
     46             data = read_into_data(argv[i]);
     47             ptr = data->data();
     48             len = data->size();
     49         }
     50         if (!L.runCode(ptr, len)) {
     51             SkDebugf("failed to load %s\n", argv[i]);
     52             exit(-1);
     53         }
     54         SkSafeUnref(data);
     55     }
     56     return 0;
     57 }
     58 
     59 #if !defined SK_BUILD_FOR_IOS
     60 int main(int argc, char * const argv[]) {
     61     return tool_main(argc, (char**) argv);
     62 }
     63 #endif
     64