<commit_before>// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {defer} from '../base/deferred';
import * as init_trace_processor from '../gen/trace_processor';

function writeToUIConsole(line: string) {
  console.log(line);
}

export interface WasmBridgeRequest {
  id: number;
  serviceName: string;
  methodName: string;
  data: Uint8Array;
}

export interface WasmBridgeResponse {
  id: number;
  success: boolean;
  data?: Uint8Array;
}

export class WasmBridge {
  // When this promise has resolved it is safe to call callWasm.
  whenInitialized: Promise<void>;

  private aborted: boolean;
  private currentRequestResult: WasmBridgeResponse|null = null;
  private connection: init_trace_processor.Module;

  constructor(init: init_trace_processor.InitWasm) {
    this.aborted = false;
    this.currentRequestResult = null;

    const deferredRuntimeInitialized = defer<void>();
    this.connection = init({
      locateFile: (s: string) => s, print: writeToUIConsole,
                      printErr: writeToUIConsole,
                      onRuntimeInitialized: () =>
                          deferredRuntimeInitialized.resolve(),
                      onAbort: () => this.aborted = true;
    });
    this.whenInitialized = deferredRuntimeInitialized.then(() => {
      const fn = this.connection.addFunction(this.onReply.bind(this), 'viiii');
      this.connection.ccall('Initialize', 'void', ['number'], [fn]);
    });
  }

  callWasm(req: WasmBridgeRequest): WasmBridgeResponse {
    if (this.aborted) {
      return {
        id: req.id,
        success: false,
        data: undefined,
      };
    }

    this.connection.ccall(
        `${req.serviceName}_${req.methodName}`,  // C method name.
        'void',                                  // Return type.
        ['number', 'array', 'number'],           // Input args.
        [req.id, req.data, req.data.length]      // Args.
        );

    const result = this.currentRequestResult;
    if (result === null) throw new Error('No result');
    if (req.id !== result.id) throw new Error(`Id ${req.id} !== ${result.id}`);
    this.currentRequestResult = null;
    return result;
  }

  // This is invoked from ccall in the same call stack as callWasm.
  private onReply(
      reqId: number, success: boolean, heapPtr: number, size: number) {
    const data = this.connection.HEAPU8.slice(heapPtr, heapPtr + size);
    this.currentRequestResult = {
      id: reqId,
      success,
      data,
    };
  }
}
<commit_msg>perfetto-ui: Simplify WASM bridge missing changes am: aa08de6fe9<commit_after>// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {defer} from '../base/deferred';
import {assertExists, assertTrue} from '../base/logging';
import * as init_trace_processor from '../gen/trace_processor';

function writeToUIConsole(line: string) {
  console.log(line);
}

export interface WasmBridgeRequest {
  id: number;
  serviceName: string;
  methodName: string;
  data: Uint8Array;
}

export interface WasmBridgeResponse {
  id: number;
  success: boolean;
  data?: Uint8Array;
}

export class WasmBridge {
  // When this promise has resolved it is safe to call callWasm.
  whenInitialized: Promise<void>;

  private aborted: boolean;
  private currentRequestResult: WasmBridgeResponse|null;
  private connection: init_trace_processor.Module;

  constructor(init: init_trace_processor.InitWasm) {
    this.aborted = false;
    this.currentRequestResult = null;

    const deferredRuntimeInitialized = defer<void>();
    this.connection = init({
      locateFile: (s: string) => s,
      print: writeToUIConsole,
      printErr: writeToUIConsole,
      onRuntimeInitialized: () => deferredRuntimeInitialized.resolve(),
      onAbort: () => this.aborted = true,
    });
    this.whenInitialized = deferredRuntimeInitialized.then(() => {
      const fn = this.connection.addFunction(this.onReply.bind(this), 'viiii');
      this.connection.ccall('Initialize', 'void', ['number'], [fn]);
    });
  }

  callWasm(req: WasmBridgeRequest): WasmBridgeResponse {
    if (this.aborted) {
      return {
        id: req.id,
        success: false,
        data: undefined,
      };
    }

    this.connection.ccall(
        `${req.serviceName}_${req.methodName}`,  // C method name.
        'void',                                  // Return type.
        ['number', 'array', 'number'],           // Input args.
        [req.id, req.data, req.data.length]      // Args.
        );

    const result = assertExists(this.currentRequestResult);
    assertTrue(req.id === result.id);
    this.currentRequestResult = null;
    return result;
  }

  // This is invoked from ccall in the same call stack as callWasm.
  private onReply(
      reqId: number, success: boolean, heapPtr: number, size: number) {
    const data = this.connection.HEAPU8.slice(heapPtr, heapPtr + size);
    this.currentRequestResult = {
      id: reqId,
      success,
      data,
    };
  }
}

{'fraction_non_alphanumeric': 0.11591096455070074, 'fraction_numerical': 0.004286892003297609, 'mean_word_length': 2.8909557408595252, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2825423', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1720, 'n_words': 524}