Win32 API 日本語リファレンス
ホームSystem.Js › JsRunScript

JsRunScript

関数
指定したJavaScriptスクリプトを解析して実行し、結果を返す。
DLLchakra.dll呼出規約winapi

シグネチャ

// chakra.dll
#include <windows.h>

JsErrorCode JsRunScript(
    LPCWSTR script,
    UINT_PTR sourceContext,
    LPCWSTR sourceUrl,
    void** result
);

パラメーター

名前方向説明
scriptLPCWSTRin解析して実行するJavaScriptソースコード文字列(Unicode)。
sourceContextUINT_PTRinこのスクリプトを識別するためのソースコンテキスト識別子(ホスト定義)。
sourceUrlLPCWSTRinデバッグやエラー表示で使うソースのURLまたはファイル名(Unicode)。
resultvoid**outスクリプト実行結果の値を受け取るポインタのアドレス。NULL可。

戻り値の型: JsErrorCode

各言語での呼び出し定義

// chakra.dll
#include <windows.h>

JsErrorCode JsRunScript(
    LPCWSTR script,
    UINT_PTR sourceContext,
    LPCWSTR sourceUrl,
    void** result
);
[DllImport("chakra.dll", ExactSpelling = true)]
static extern uint JsRunScript(
    [MarshalAs(UnmanagedType.LPWStr)] string script,   // LPCWSTR
    UIntPtr sourceContext,   // UINT_PTR
    [MarshalAs(UnmanagedType.LPWStr)] string sourceUrl,   // LPCWSTR
    IntPtr result   // void** out
);
<DllImport("chakra.dll", ExactSpelling:=True)>
Public Shared Function JsRunScript(
    <MarshalAs(UnmanagedType.LPWStr)> script As String,   ' LPCWSTR
    sourceContext As UIntPtr,   ' UINT_PTR
    <MarshalAs(UnmanagedType.LPWStr)> sourceUrl As String,   ' LPCWSTR
    result As IntPtr   ' void** out
) As UInteger
End Function
' script : LPCWSTR
' sourceContext : UINT_PTR
' sourceUrl : LPCWSTR
' result : void** out
Declare PtrSafe Function JsRunScript Lib "chakra" ( _
    ByVal script As LongPtr, _
    ByVal sourceContext As LongPtr, _
    ByVal sourceUrl As LongPtr, _
    ByVal result As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

JsRunScript = ctypes.windll.chakra.JsRunScript
JsRunScript.restype = wintypes.DWORD
JsRunScript.argtypes = [
    wintypes.LPCWSTR,  # script : LPCWSTR
    ctypes.c_size_t,  # sourceContext : UINT_PTR
    wintypes.LPCWSTR,  # sourceUrl : LPCWSTR
    ctypes.c_void_p,  # result : void** out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('chakra.dll')
JsRunScript = Fiddle::Function.new(
  lib['JsRunScript'],
  [
    Fiddle::TYPE_VOIDP,  # script : LPCWSTR
    Fiddle::TYPE_UINTPTR_T,  # sourceContext : UINT_PTR
    Fiddle::TYPE_VOIDP,  # sourceUrl : LPCWSTR
    Fiddle::TYPE_VOIDP,  # result : void** out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "chakra")]
extern "system" {
    fn JsRunScript(
        script: *const u16,  // LPCWSTR
        sourceContext: usize,  // UINT_PTR
        sourceUrl: *const u16,  // LPCWSTR
        result: *mut *mut ()  // void** out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("chakra.dll")]
public static extern uint JsRunScript([MarshalAs(UnmanagedType.LPWStr)] string script, UIntPtr sourceContext, [MarshalAs(UnmanagedType.LPWStr)] string sourceUrl, IntPtr result);
"@
$api = Add-Type -MemberDefinition $sig -Name 'chakra_JsRunScript' -Namespace Win32 -PassThru
# $api::JsRunScript(script, sourceContext, sourceUrl, result)
#uselib "chakra.dll"
#func global JsRunScript "JsRunScript" sptr, sptr, sptr, sptr
; JsRunScript script, sourceContext, sourceUrl, result   ; 戻り値は stat
; script : LPCWSTR -> "sptr"
; sourceContext : UINT_PTR -> "sptr"
; sourceUrl : LPCWSTR -> "sptr"
; result : void** out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "chakra.dll"
#cfunc global JsRunScript "JsRunScript" wstr, sptr, wstr, sptr
; res = JsRunScript(script, sourceContext, sourceUrl, result)
; script : LPCWSTR -> "wstr"
; sourceContext : UINT_PTR -> "sptr"
; sourceUrl : LPCWSTR -> "wstr"
; result : void** out -> "sptr"
; JsErrorCode JsRunScript(LPCWSTR script, UINT_PTR sourceContext, LPCWSTR sourceUrl, void** result)
#uselib "chakra.dll"
#cfunc global JsRunScript "JsRunScript" wstr, intptr, wstr, intptr
; res = JsRunScript(script, sourceContext, sourceUrl, result)
; script : LPCWSTR -> "wstr"
; sourceContext : UINT_PTR -> "intptr"
; sourceUrl : LPCWSTR -> "wstr"
; result : void** out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	chakra = windows.NewLazySystemDLL("chakra.dll")
	procJsRunScript = chakra.NewProc("JsRunScript")
)

// script (LPCWSTR), sourceContext (UINT_PTR), sourceUrl (LPCWSTR), result (void** out)
r1, _, err := procJsRunScript.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(script))),
	uintptr(sourceContext),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(sourceUrl))),
	uintptr(result),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // JsErrorCode
function JsRunScript(
  script: PWideChar;   // LPCWSTR
  sourceContext: NativeUInt;   // UINT_PTR
  sourceUrl: PWideChar;   // LPCWSTR
  result: Pointer   // void** out
): DWORD; stdcall;
  external 'chakra.dll' name 'JsRunScript';
result := DllCall("chakra\JsRunScript"
    , "WStr", script   ; LPCWSTR
    , "UPtr", sourceContext   ; UINT_PTR
    , "WStr", sourceUrl   ; LPCWSTR
    , "Ptr", result   ; void** out
    , "UInt")   ; return: JsErrorCode
●JsRunScript(script, sourceContext, sourceUrl, result) = DLL("chakra.dll", "dword JsRunScript(char*, int, char*, void*)")
# 呼び出し: JsRunScript(script, sourceContext, sourceUrl, result)
# script : LPCWSTR -> "char*"
# sourceContext : UINT_PTR -> "int"
# sourceUrl : LPCWSTR -> "char*"
# result : void** out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "chakra" fn JsRunScript(
    script: [*c]const u16, // LPCWSTR
    sourceContext: usize, // UINT_PTR
    sourceUrl: [*c]const u16, // LPCWSTR
    result: ?*anyopaque // void** out
) callconv(std.os.windows.WINAPI) u32;
proc JsRunScript(
    script: WideCString,  # LPCWSTR
    sourceContext: uint,  # UINT_PTR
    sourceUrl: WideCString,  # LPCWSTR
    result: pointer  # void** out
): uint32 {.importc: "JsRunScript", stdcall, dynlib: "chakra.dll".}
pragma(lib, "chakra");
extern(Windows)
uint JsRunScript(
    const(wchar)* script,   // LPCWSTR
    size_t sourceContext,   // UINT_PTR
    const(wchar)* sourceUrl,   // LPCWSTR
    void** result   // void** out
);
ccall((:JsRunScript, "chakra.dll"), stdcall, UInt32,
      (Cwstring, Csize_t, Cwstring, Ptr{Cvoid}),
      script, sourceContext, sourceUrl, result)
# script : LPCWSTR -> Cwstring
# sourceContext : UINT_PTR -> Csize_t
# sourceUrl : LPCWSTR -> Cwstring
# result : void** out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t JsRunScript(
    const uint16_t* script,
    uintptr_t sourceContext,
    const uint16_t* sourceUrl,
    void** result);
]]
local chakra = ffi.load("chakra")
-- chakra.JsRunScript(script, sourceContext, sourceUrl, result)
-- script : LPCWSTR
-- sourceContext : UINT_PTR
-- sourceUrl : LPCWSTR
-- result : void** out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('chakra.dll');
const JsRunScript = lib.func('__stdcall', 'JsRunScript', 'uint32_t', ['str16', 'uintptr_t', 'str16', 'void *']);
// JsRunScript(script, sourceContext, sourceUrl, result)
// script : LPCWSTR -> 'str16'
// sourceContext : UINT_PTR -> 'uintptr_t'
// sourceUrl : LPCWSTR -> 'str16'
// result : void** out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("chakra.dll", {
  JsRunScript: { parameters: ["buffer", "usize", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.JsRunScript(script, sourceContext, sourceUrl, result)
// script : LPCWSTR -> "buffer"
// sourceContext : UINT_PTR -> "usize"
// sourceUrl : LPCWSTR -> "buffer"
// result : void** out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t JsRunScript(
    const uint16_t* script,
    size_t sourceContext,
    const uint16_t* sourceUrl,
    void** result);
C, "chakra.dll");
// $ffi->JsRunScript(script, sourceContext, sourceUrl, result);
// script : LPCWSTR
// sourceContext : UINT_PTR
// sourceUrl : LPCWSTR
// result : void** out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Chakra extends StdCallLibrary {
    Chakra INSTANCE = Native.load("chakra", Chakra.class);
    int JsRunScript(
        WString script,   // LPCWSTR
        long sourceContext,   // UINT_PTR
        WString sourceUrl,   // LPCWSTR
        Pointer result   // void** out
    );
}
@[Link("chakra")]
lib Libchakra
  fun JsRunScript = JsRunScript(
    script : UInt16*,   # LPCWSTR
    sourceContext : LibC::SizeT,   # UINT_PTR
    sourceUrl : UInt16*,   # LPCWSTR
    result : Void**   # void** out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef JsRunScriptNative = Uint32 Function(Pointer<Utf16>, UintPtr, Pointer<Utf16>, Pointer<Void>);
typedef JsRunScriptDart = int Function(Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Void>);
final JsRunScript = DynamicLibrary.open('chakra.dll')
    .lookupFunction<JsRunScriptNative, JsRunScriptDart>('JsRunScript');
// script : LPCWSTR -> Pointer<Utf16>
// sourceContext : UINT_PTR -> UintPtr
// sourceUrl : LPCWSTR -> Pointer<Utf16>
// result : void** out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function JsRunScript(
  script: PWideChar;   // LPCWSTR
  sourceContext: NativeUInt;   // UINT_PTR
  sourceUrl: PWideChar;   // LPCWSTR
  result: Pointer   // void** out
): DWORD; stdcall;
  external 'chakra.dll' name 'JsRunScript';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "JsRunScript"
  c_JsRunScript :: CWString -> CUIntPtr -> CWString -> Ptr () -> IO Word32
-- script : LPCWSTR -> CWString
-- sourceContext : UINT_PTR -> CUIntPtr
-- sourceUrl : LPCWSTR -> CWString
-- result : void** out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let jsrunscript =
  foreign "JsRunScript"
    ((ptr uint16_t) @-> size_t @-> (ptr uint16_t) @-> (ptr void) @-> returning uint32_t)
(* script : LPCWSTR -> (ptr uint16_t) *)
(* sourceContext : UINT_PTR -> size_t *)
(* sourceUrl : LPCWSTR -> (ptr uint16_t) *)
(* result : void** out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library chakra (t "chakra.dll"))
(cffi:use-foreign-library chakra)

(cffi:defcfun ("JsRunScript" js-run-script :convention :stdcall) :uint32
  (script (:string :encoding :utf-16le))   ; LPCWSTR
  (source-context :uint64)   ; UINT_PTR
  (source-url (:string :encoding :utf-16le))   ; LPCWSTR
  (result :pointer))   ; void** out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $JsRunScript = Win32::API::More->new('chakra',
    'DWORD JsRunScript(LPCWSTR script, WPARAM sourceContext, LPCWSTR sourceUrl, LPVOID result)');
# my $ret = $JsRunScript->Call($script, $sourceContext, $sourceUrl, $result);
# script : LPCWSTR -> LPCWSTR
# sourceContext : UINT_PTR -> WPARAM
# sourceUrl : LPCWSTR -> LPCWSTR
# result : void** out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型