Commit 09fc119a authored by Jonathan Kew's avatar Jonathan Kew
Browse files

Bug 1590167 - Add Rust implementation of hyphenation (mapped_hyph) and hook up...

Bug 1590167 - Add Rust implementation of hyphenation (mapped_hyph) and hook up in place of libhyphen. r=heycam

Differential Revision: https://phabricator.services.mozilla.com/D49967

--HG--
extra : moz-landing-system : lando
parent 122d3025
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -7,6 +7,11 @@ branch = "master"
git = "https://github.com/mozilla/neqo"
replace-with = "vendored-sources"

[source."https://github.com/jfkthame/mapped_hyph.git"]
branch = "master"
git = "https://github.com/jfkthame/mapped_hyph.git"
replace-with = "vendored-sources"

[source."https://github.com/hsivonen/packed_simd"]
branch = "rust_1_32"
git = "https://github.com/hsivonen/packed_simd"
+12 −1
Original line number Diff line number Diff line
@@ -1239,6 +1239,7 @@ dependencies = [
 "kvstore 0.1.0",
 "lmdb-rkv-sys 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "mapped_hyph 0.2.0 (git+https://github.com/jfkthame/mapped_hyph.git)",
 "mdns_service 0.1.0",
 "mozurl 0.0.1",
 "mp4parse_capi 0.11.2",
@@ -1706,6 +1707,15 @@ dependencies = [
 "synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "mapped_hyph"
version = "0.2.0"
source = "git+https://github.com/jfkthame/mapped_hyph.git#943b04631796b88b8e9d9a6932cc9258500f8239"
dependencies = [
 "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "memmap 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "marionette"
version = "0.1.0"
@@ -2723,7 +2733,7 @@ dependencies = [
 "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "murmurhash3 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
@@ -4104,6 +4114,7 @@ dependencies = [
"checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084"
"checksum mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa"
"checksum malloc_size_of_derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "35adee9ed962cf7d07d62cb58bc45029f3227f5b5b86246caa8632f06c187bc3"
"checksum mapped_hyph 0.2.0 (git+https://github.com/jfkthame/mapped_hyph.git)" = "<none>"
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
"checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39"
"checksum memmap 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b"

intl/hyphenation/glue/hnjalloc.h

deleted100644 → 0
+0 −46
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * To enable us to load hyphenation dictionaries from arbitrary resource URIs,
 * not just through file paths using stdio, we override the (few) stdio APIs
 * that hyphen.c uses and provide our own reimplementation that calls Gecko
 * i/o methods.
 */

#include <stdio.h> /* ensure stdio.h is loaded before our macros */

#undef FILE
#define FILE hnjFile

#define fopen(path, mode) hnjFopen(path, mode)
#define fclose(file) hnjFclose(file)
#define fgets(buf, count, file) hnjFgets(buf, count, file)
#define feof(file) hnjFeof(file)
#define fgetc(file) hnjFgetc(file)

typedef struct hnjFile_ hnjFile;

#ifdef __cplusplus
extern "C" {
#endif

void* hnj_malloc(size_t size);
void* hnj_realloc(void* ptr, size_t size);
void hnj_free(void* ptr);

hnjFile* hnjFopen(const char* aURISpec, const char* aMode);

int hnjFclose(hnjFile* f);

char* hnjFgets(char* s, int n, hnjFile* f);

int hnjFeof(hnjFile* f);

int hnjFgetc(hnjFile* f);

#ifdef __cplusplus
}
#endif
+0 −133
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This file provides substitutes for the basic stdio routines used by hyphen.c
// to read its dictionary files. We #define the stdio names to these versions
// in hnjalloc.h, so that we can use nsIURI and nsIInputStream to specify and
// access the dictionary resources.

#include "hnjalloc.h"

#undef FILE  // Undo #defines from hnjalloc.h before #including other headers
#undef fopen
#undef fclose
#undef fgets
#undef feof
#undef fgetc

#include "nsNetUtil.h"
#include "nsIInputStream.h"
#include "nsIURI.h"
#include "nsContentUtils.h"

#define BUFSIZE 1024

struct hnjFile_ {
  nsCOMPtr<nsIInputStream> mStream;
  char mBuffer[BUFSIZE];
  uint32_t mCurPos;
  uint32_t mLimit;
  bool mEOF;
};

// replacement for fopen()
// (not a full substitute: only supports read access)
hnjFile* hnjFopen(const char* aURISpec, const char* aMode) {
  // this override only needs to support "r"
  NS_ASSERTION(!strcmp(aMode, "r"), "unsupported fopen() mode in hnjFopen");

  nsCOMPtr<nsIURI> uri;
  nsresult rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
  if (NS_FAILED(rv)) {
    return nullptr;
  }

  nsCOMPtr<nsIChannel> channel;
  rv = NS_NewChannel(getter_AddRefs(channel), uri,
                     nsContentUtils::GetSystemPrincipal(),
                     nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
                     nsIContentPolicy::TYPE_OTHER);
  if (NS_FAILED(rv)) {
    return nullptr;
  }

  nsCOMPtr<nsIInputStream> instream;
  rv = channel->Open(getter_AddRefs(instream));
  if (NS_FAILED(rv)) {
    return nullptr;
  }

  hnjFile* f = new hnjFile;
  f->mStream = instream;
  f->mCurPos = 0;
  f->mLimit = 0;
  f->mEOF = false;

  return f;
}

// replacement for fclose()
int hnjFclose(hnjFile* f) {
  NS_ASSERTION(f && f->mStream, "bad argument to hnjFclose");

  int result = 0;
  nsresult rv = f->mStream->Close();
  if (NS_FAILED(rv)) {
    result = EOF;
  }
  f->mStream = nullptr;

  delete f;
  return result;
}

// replacement for fgetc()
int hnjFgetc(hnjFile* f) {
  if (f->mCurPos >= f->mLimit) {
    f->mCurPos = 0;

    nsresult rv = f->mStream->Read(f->mBuffer, BUFSIZE, &f->mLimit);
    if (NS_FAILED(rv)) {
      f->mLimit = 0;
    }

    if (f->mLimit == 0) {
      f->mEOF = true;
      return EOF;
    }
  }

  return f->mBuffer[f->mCurPos++];
}

// replacement for fgets()
// (not a full reimplementation, but sufficient for libhyphen's needs)
char* hnjFgets(char* s, int n, hnjFile* f) {
  NS_ASSERTION(s && f, "bad argument to hnjFgets");

  int i = 0;
  while (i < n - 1) {
    int c = hnjFgetc(f);

    if (c == EOF) {
      break;
    }

    s[i++] = c;

    if (c == '\n' || c == '\r') {
      break;
    }
  }

  if (i == 0) {
    return nullptr;  // end of file
  }

  s[i] = '\0';  // null-terminate the returned string
  return s;
}

int hnjFeof(hnjFile* f) { return f->mEOF ? EOF : 0; }
+10 −9
Original line number Diff line number Diff line
@@ -14,16 +14,17 @@ UNIFIED_SOURCES += [
    'nsHyphenator.cpp',
]

# These files cannot be built in unified mode because they include hnjalloc.h.
SOURCES += [
    'hnjstdio.cpp',
]

LOCAL_INCLUDES += [
    '../hyphen',
]

FINAL_LIBRARY = 'xul'

if CONFIG['CC_TYPE'] in ('clang', 'gcc'):
    CXXFLAGS += ['-Wno-error=shadow']

GENERATED_FILES += [
    'mapped_hyph.h'
]

generated = GENERATED_FILES['mapped_hyph.h']
generated.script = '/layout/style/RunCbindgen.py:generate'
generated.inputs = [
    '/third_party/rust/mapped_hyph'
]
Loading