/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is js-ctypes.
 *
 * The Initial Developer of the Original Code is
 * The Mozilla Foundation <http://www.mozilla.org/>.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *  Mark Finkle <mark.finkle@gmail.com>, <mfinkle@mozilla.com>
 *  Dan Witte <dwitte@mozilla.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsISupports.idl"

interface nsILocalFile;

/**
 * Interface that wraps a dynamic library and can create
 * callable 'method' objects from exportable functions in the library
 */
[scriptable, uuid(352a72c1-5b99-451e-a330-c45079d8f087)]
interface nsIForeignLibrary : nsISupports
{
  /**
   * ABI constants that specify the calling convention to use.
   * DEFAULT corresponds to the cdecl convention, and in almost all
   * cases is the correct choice. STDCALL is provided for calling
   * functions in the Microsoft Win32 API.
   */
  const PRUint16 DEFAULT    = 0;    // corresponds to cdecl
  const PRUint16 STDCALL    = 1;    // for calling Win32 API functions

  /**
   * Types available for arguments and return values, representing
   * their C counterparts.
   */
  const PRUint16 VOID       = 0;    // Only allowed for return types.
  const PRUint16 BOOL       = 1;    // _Bool type (assumed 8 bits wide).
  const PRUint16 INT8       = 2;    // int8_t (signed char) type.
  const PRUint16 INT16      = 3;    // int16_t (short) type.
  const PRUint16 INT32      = 4;    // int32_t (int) type.
  const PRUint16 INT64      = 5;    // int64_t (long long) type.
  const PRUint16 UINT8      = 6;    // uint8_t (unsigned char) type.
  const PRUint16 UINT16     = 7;    // uint16_t (unsigned short) type.
  const PRUint16 UINT32     = 8;    // uint32_t (unsigned int) type.
  const PRUint16 UINT64     = 9;    // uint64_t (unsigned long long) type.
  const PRUint16 FLOAT      = 10;   // float type.
  const PRUint16 DOUBLE     = 11;   // double type.
  const PRUint16 STRING     = 12;   // C string (char *).
  const PRUint16 USTRING    = 13;   // 16-bit string (char16_t *).

  /**
   * Attempts to dynamically load the specified library
   */
  void open(in nsILocalFile aFile);

  /**
   * Unloads the currently loaded library. Any subsequent attempts to call
   * functions on this interface will fail.
   */
  void close();

  /**
   * Used to specify an exported method from the currently loaded library. Even
   * though the method appears to take no parameters, it does in fact require
   * several. It uses some XPCOM/JSAPI magic to extract the parameters.
   *
   * declare(in AString aName, in int aABI, in int aReturnType, in int aArgType1, ...)
   *
   * The resulting object can then be used to call the underlying
   * C function like so:
   *
   * var retval = nativeMethod(arg1, arg2, ...);
   *
   * Arguments will be checked against the types supplied at declaration, and
   * some attempt to convert values (e.g. boolean true/false to integer 0/1)
   * will be made. Otherwise, if types do not match, or conversion fails,
   * an exception will be thrown.
   */
  nsISupports declare();
};
