Example #1
0
CachedUnit createUnitFromString(const char* path,
                                const String& contents,
                                Unit** releaseUnit,
                                OptLog& ent,
                                const Native::FuncTable& nativeFuncs,
                                const RepoOptions& options,
                                FileLoadFlags& flags) {
  auto const sha1 = SHA1{mangleUnitSha1(string_sha1(contents.slice()),
                                        options)};
  // Try the repo; if it's not already there, invoke the compiler.
  if (auto unit = Repo::get().loadUnit(path, sha1, nativeFuncs)) {
    flags = FileLoadFlags::kHitDisk;
    return CachedUnit { unit.release(), rds::allocBit() };
  }
  LogTimer compileTimer("compile_ms", ent);
  rqtrace::EventGuard trace{"COMPILE_UNIT"};
  trace.annotate("file_size", folly::to<std::string>(contents.size()));
  flags = FileLoadFlags::kCompiled;
  auto const unit = compile_file(contents.data(), contents.size(), sha1, path,
                                 nativeFuncs, options, releaseUnit);
  return CachedUnit { unit, rds::allocBit() };
}
String StringUtil::SHA1(CStrRef input, bool raw /* = false */) {
  int len;
  char *ret = string_sha1(input.data(), input.size(), raw, len);
  return String(ret, len, AttachString);
}